日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

SSM的整合

發(fā)布時(shí)間:2025/3/20 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSM的整合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

一、整合分析

整合的示例:

整合的配置方式:

整合步驟分析:

二、SSM整合

1.建表

2.創(chuàng)建表對(duì)應(yīng)的實(shí)體對(duì)象

3.創(chuàng)建Service層的java類(lèi)

4.搭建Spring環(huán)境

(1)導(dǎo)入Spring的pom坐標(biāo)

(2)Spring的配置文件

(3)測(cè)試Spring能否獨(dú)立運(yùn)行

5.搭建MyBatis環(huán)境

(1)導(dǎo)入MyBatis的pom依賴(lài)

(2)MyBatis的配置文件

(3)編寫(xiě)Account對(duì)應(yīng)的Dao層

(4)測(cè)試MyBatis能否獨(dú)立運(yùn)行

6.整合Spring和MyBatis

(1)導(dǎo)入整合的pom依賴(lài)

(2)Spring接管MyBatis

(3)改造Service

7.Junit測(cè)試整合結(jié)果

(1)導(dǎo)入pom依賴(lài)

(2)測(cè)試整合結(jié)果

8.搭建SpringMVC環(huán)境

(1)導(dǎo)入Spring的pom依賴(lài)

(2)配置web.xml

(3)配置springmvc.xml

(4)編寫(xiě)AccountController

(5)編寫(xiě)測(cè)試頁(yè)面

(6)開(kāi)啟tomcat測(cè)試

9.整合Spring和SpringMVC

(1)配置web.xml

(2)改造AccountController


一、整合分析

整合的示例:

Account的保存和列表查詢(xún)

整合的配置方式:

Mybatis:XML+注解的方式(最終只有注解) Spring:XML+注解的方式(我們自己寫(xiě)的類(lèi)用注解,別人寫(xiě)的用XML) SpringMVC:XML+注解的方式(和spring的配置思想是一樣的)

整合步驟分析:

第一步:保證spring框架可以在maven工程中獨(dú)立運(yùn)行(Spring的IOC環(huán)境搭建) 第二步:保證mybatis框架可以在maven工程中獨(dú)立運(yùn)行(Mybatis的環(huán)境搭建) 第三步:整合spring和mybatis 思路:讓spring接管SqlSessionFactory的創(chuàng)建以及spring接管代理dao實(shí)現(xiàn)類(lèi)的創(chuàng)建 第四步:通過(guò)整合Junit測(cè)試spring和mybatis的整合結(jié)果 第五步:保證springmvc框架可以在maven工程中獨(dú)立運(yùn)行(SpringMVC環(huán)境搭建) 第六步:整合spring和springmv,把service對(duì)象注入到controller中

二、SSM整合

1.建表

create table account(id int primary key auto_increment,name varchar(40),money float )character set utf8 collate utf8_general_ci;insert into account(name,money) values('aaa',1000); insert into account(name,money) values('bbb',1000); insert into account(name,money) values('ccc',1000);

2.創(chuàng)建表對(duì)應(yīng)的實(shí)體對(duì)象

public class Account {private Integer id;private String name;private Float money; }

3.創(chuàng)建Service層的java類(lèi)

public interface IAccountService {/*** 保存賬戶(hù)** @param account*/void saveAccount(Account account); ?/*** 查詢(xún)所有賬戶(hù)** @return*/List<Account> findAllAccount(); } @Service("accountService") public class AccountServiceImpl implements IAccountService { ?@Overridepublic void saveAccount(Account account) {System.out.println("savaAccount方法執(zhí)行了");} ?@Overridepublic List<Account> findAllAccount() {System.out.println("findAllAccount方法執(zhí)行了");return null;} }

4.搭建Spring環(huán)境

(1)導(dǎo)入Spring的pom坐標(biāo)

? ? ? ?<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.4.RELEASE</version></dependency>

(2)Spring的配置文件

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ?<!-- 配置spring創(chuàng)建容器時(shí)要掃描的包 --><context:component-scan base-package="com.itheima"><!--制定掃包規(guī)則,不掃描@Controller注解的JAVA類(lèi),其他的還是要掃描 --><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan></beans>

(3)測(cè)試Spring能否獨(dú)立運(yùn)行

public class Test01Spring {public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);accountService.findAllAccount();accountService.saveAccount(new Account());} } 打印 結(jié)果如下: findAllAccount方法執(zhí)行了 savaAccount方法執(zhí)行了

5.搭建MyBatis環(huán)境

(1)導(dǎo)入MyBatis的pom依賴(lài)

? ? ? ?<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- 此次整合用druid的連接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency>

(2)MyBatis的配置文件

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><!-- 使用properties來(lái)引入外部properties配置文件的內(nèi)容--><properties resource="jdbcConfig.properties"></properties><environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"></transactionManager><dataSource type="pooled"><property name="driver" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></dataSource></environment></environments><mappers><!-- 自動(dòng)掃描指定包下的Dao --><package name="com.itheima.dao"/></mappers> </configuration>

druid.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm jdbc.username=root jdbc.password=123456

(3)編寫(xiě)Account對(duì)應(yīng)的Dao層

AccountDao.java

@Repository public interface AccountDao {/*** 保存** @param account*/void save(Account account); ?/*** 查詢(xún)所有** @return*/List<Account> findAll(); }

AccountDao.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.dao.AccountDao"><!-- 查詢(xún)所有賬戶(hù) --><select id="findAll" resultType="com.itheima.domain.Account">select * from account</select><!-- 新增賬戶(hù) --><insert id="save" parameterType="com.itheima.domain.Account">insert into account(name,money)values(#{name},#{money})</insert> </mapper>

(4)測(cè)試MyBatis能否獨(dú)立運(yùn)行

public class Test01MyBatis {private static SqlSessionFactory getSqlSessionFactory() throws IOException {//1.根據(jù)xml配置文件創(chuàng)建一個(gè)SqlSessionFactory對(duì)象String resource = "conf/mybatis-config.xml";InputStream resourceAsStream = Resources.getResourceAsStream(resource);return new SqlSessionFactoryBuilder().build(resourceAsStream);}@Testpublic void test01() throws Exception {//1.獲取SqlSessionFactorySqlSessionFactory sqlSessionFactory = getSqlSessionFactory();//2.獲取sqlSession對(duì)象SqlSession session = sqlSessionFactory.openSession();//3.獲取接口的實(shí)現(xiàn)類(lèi)對(duì)象AccountDao aDao = session.getMapper(AccountDao.class);Account account = new Account();//4.測(cè)試保存方法account.setName("test");account.setMoney(5000f);aDao.save(account);//5.測(cè)試查詢(xún)方法List<Account> list = aDao.findAll();System.out.println(list);session.commit();session.close();in.close();}

6.整合Spring和MyBatis

(1)導(dǎo)入整合的pom依賴(lài)

? ? ? ?<!--導(dǎo)入spring和mybatis整合的坐標(biāo)--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.4.RELEASE</version></dependency> ?<!--配置aspectJ語(yǔ)言的解析坐標(biāo)--><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency>

(2)Spring接管MyBatis

  • SqlMapConfig.xml配置文件中的東西可以刪除,在Spring配置文件中配置

  • 以后也可以在SqlMapConfig.xml配置文件中進(jìn)行某些配置,和Spring的配置文件共同生效

applicationContext.xml

? ?<!-- 加載配置文件 --><context:property-placeholder location="classpath:druid.properties"/><!-- 配置MyBatis的Session工廠 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數(shù)據(jù)庫(kù)連接池 --><property name="dataSource" ref="dataSource"/><!-- 加載mybatis的全局配置文件 --><property name="configLocation" value="classpath:SqlMapConfig.xml"/></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean> ? ?<!-- 配置Mapper掃描器 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.itheima.dao"></property></bean> ?<!-- 配置事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事務(wù)的通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 配置aop --><aop:config><!-- 配置切入點(diǎn)表達(dá)式 --><aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/><!-- 建立通知和切入點(diǎn)表達(dá)式的關(guān)系 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/></aop:config>

(3)改造Service

@Service("accountService") public class AccountServiceImpl implements IAccountService {//將acountDao注入到AccountServiceImpl中@AutowiredAccountDao accountDao; ?@Overridepublic void saveAccount(Account account) {accountDao.save(account);} ?@Overridepublic List<Account> findAllAccount() {List<Account> accounts = accountDao.findAll();return accounts;} }

7.Junit測(cè)試整合結(jié)果

(1)導(dǎo)入pom依賴(lài)

? ? ? ?<!--導(dǎo)入spring和Junit的整合坐標(biāo)--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.4.RELEASE</version></dependency>

(2)測(cè)試整合結(jié)果

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class TestSpringMyBatis {@Autowiredprivate IAccountService accountService; ?@Testpublic void testFindAll() {List list = accountService.findAllAccount();System.out.println(list);} ?@Testpublic void testSave() {Account account = new Account();account.setName("測(cè)試賬號(hào)");account.setMoney(1234f);accountService.saveAccount(account);} }

8.搭建SpringMVC環(huán)境

(1)導(dǎo)入Spring的pom依賴(lài)

? ? ? ?<!--導(dǎo)入springmvc和servlet api的坐標(biāo)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.4.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version></dependency>

(2)配置web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"><display-name>ssm_web</display-name><!-- 配置spring mvc的核心控制器 --><servlet><servlet-name>springmvcDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置初始化參數(shù),用于讀取springmvc的配置文件 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 配置servlet的對(duì)象的創(chuàng)建時(shí)間點(diǎn):應(yīng)用加載時(shí)創(chuàng)建。取值只能是非0正整數(shù),表示啟動(dòng)順序 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvcDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 配置springMVC編碼過(guò)濾器 --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 設(shè)置過(guò)濾器中的屬性值 --><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><!-- 啟動(dòng)過(guò)濾器 --><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><!-- 過(guò)濾所有請(qǐng)求 --><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

(3)配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> ?<!-- 配置創(chuàng)建spring容器要掃描的包 --><context:component-scan base-package="com.itheima.controller"></context:component-scan> ?<!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean> ?<!-- 開(kāi)啟springmvc對(duì)注解的支持 --><mvc:annotation-driven></mvc:annotation-driven> </beans>

(4)編寫(xiě)AccountController

@Controller @RequestMapping("account") public class AccountController { ?@RequestMapping("findAllAccount")public String findAllAccount(){System.out.println("執(zhí)行了查詢(xún)賬號(hào)");return "success";} } ?

(5)編寫(xiě)測(cè)試頁(yè)面

find.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>主頁(yè)</title></head> <body><a href="account/findAllAccount">訪問(wèn)查詢(xún)賬戶(hù)</a></body> </html>

(6)開(kāi)啟tomcat測(cè)試

  • 訪問(wèn)該頁(yè)面后控制臺(tái)打出以下信息:

執(zhí)行了查詢(xún)賬號(hào)
  • 同時(shí)頁(yè)面跳轉(zhuǎn)到WEB-INF/pages下的success.jsp頁(yè)面

9.整合Spring和SpringMVC

(1)配置web.xml

? ?<!-- 配置spring提供的監(jiān)聽(tīng)器,用于啟動(dòng)服務(wù)時(shí)加載容器 --><!-- 該監(jiān)聽(tīng)器只能加載WEB-INF目錄中名稱(chēng)為applicationContext.xml的配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 手動(dòng)指定spring配置文件位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>

(2)改造AccountController

@Controller @RequestMapping("account") public class AccountController { ?//把a(bǔ)ccountService對(duì)象注入進(jìn)來(lái)@AutowiredIAccountService accountService; ?@RequestMapping("findAllAccount")public String findAllAccount(){List<Account> accounts = accountService.findAllAccount();for (Account account : accounts) {System.out.println(account);}return "success";} }

?

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的SSM的整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。