目录
一、Mybatis整合Spring
1、整合后的maven坐标
2、核心配置文件
3、pojo、mapper、service配置
4、单测
二、整合SpringMVC
1、引入springMVC的坐标并配置tomcat
2、核心配置文件
3、controller配置
4、启动项目并测试
SSM = SpringMVC + Spring + Mybatis
整合顺序:
先整合Spring+Mybatis,再整合SpringMVC
新建项目:
一、Mybatis整合Spring
整合思路:
-
数据库连接池以及事务管理都交给Spring容器来完成
-
SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
-
Mapper动态代理对象交给Spring管理
1、整合后的maven坐标
-
Junit测试jar(4.12版本)
-
Mybatis的jar(3.4.5)
-
Spring相关jar(spring-context、spring-test、spring-jdbc、spring-tx、spring-aop、
-
aspectjweaver)
-
Mybatis/Spring整合包jar(mybatis-spring-xx.jar)
-
Mysql数据库驱动jar
-
Druid数据库连接池的jar
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--spring相关-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--mybatis与spring的整合包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.3</version>
</dependency>
<!--数据库驱动jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
2、核心配置文件
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/manage
jdbc.username=root
jdbc.password=123456
applicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--包扫描-->
<context:component-scan base-package="com.test.mapper"/>
<!--数据库连接池以及事务管理都交给Spring容器来完成-->
<!--引⼊外部资源⽂件-->
<context:property-placeholder
location="classpath:jdbc.properties"/>
<!--第三⽅jar中的bean定义在xml中-->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--SqlSessionFactory对象应该放到Spring容器中作为单例对象管理
原来mybaits中sqlSessionFactory的构建是需要素材的:SqlMapConfig.xml中的内
容
-->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--别名映射扫描-->
<property name="typeAliasesPackage" value="com.test.pojo"/>
<!--数据源dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--Mapper动态代理对象交给Spring管理,我们从Spring容器中直接获得Mapper的代理对
象-->
<!--扫描mapper接⼝,⽣成代理对象,⽣成的代理对象会存储在ioc容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--mapper接⼝包路径配置-->
<property name="basePackage" value="com.test.mapper"/>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
</bean>
</beans>
applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:lgContext="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--包扫描-->
<lgContext:component-scan base-package="com.test.service"/>
<!--事务管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务管理注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3、pojo、mapper、service配置
src/main/java/com/test/pojo/User.java
public class User {
private int id;
private String username;
private String password;
}
src/main/java/com/test/mapper/UserMapper.java
public interface UserMapper {
public List<User> findList();
}
src/main/resources/com/test/mapper/UserMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.mapper.UserMapper">
<select id="findList" resultType="com.test.pojo.User">
select * from user
</select>
</mapper>
src/main/java/com/test/service/UserService.java
public interface UserService {
public List<User> findList();
}
src/main/java/com/test/service/UserServiceImpl.java
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Override
public List<User> findList() {
return userMapper.findList();
}
}
4、单测
src/test/java/MybatisSpringTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:application*.xml"})
public class MybatisSpringTest {
@Autowired
private UserService userService;
@Test
public void testMybatisSpring() throws Exception {
List<User> list = userService.findList();
System.out.println(list);
}
}
二、整合SpringMVC
1、引入springMVC的坐标并配置tomcat
<!--SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<!--jsp-api&servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--⻚⾯使⽤jstl表达式-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--json数据交互所需jar,start-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!--json数据交互所需jar,end-->
<build>
<plugins>
<!--tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
2、核心配置文件
src/main/resources/springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--扫描controller-->
<context:component-scan base-package="com.test.controller"/>
<mvc:annotation-driven/>
</beans>
src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!--spring框架启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc启动-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、controller配置
src/main/java/com/test/controller/UserContoller.java
@Controller
@RequestMapping("/user")
public class UserContoller {
@Autowired
private UserService userService;
@RequestMapping("/queryAll")
@ResponseBody
public List<User> queryAll() throws Exception {
return userService.findList();
}
}
4、启动项目并测试
页面访问测试:
http://localhost:8080/user/queryAll