目录
2.2、创建 mybatis 配置文件 mybatis-config.xml
3.2.1、spring-context.xml:只配置注解声明与类的管理
3.2.2、spring-mvc.xml:进行 mvc 相关的配置,如 静态资源配置、拦截器配置
3.2.3、spring-mybatis.xml:进行 spring 与 mybatis 整合相关的配置
4.2.3、在 spring-mybatis.xml 中配置
1、创建 web 项目
1.1、创建 Maven 工程并设置打包方式为 war 包
在 pom.xml 中设置
<packaging>war</packaging>
1.2、引入 web 项目依赖
在 pom.xml 中引入依赖
<!-- jsp -->
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
1.3、完成 Maven模块web项目结构
① 在导完依赖后,记得刷新 Maven,然后在 src 下创建 webapp
② 在项目结构中生成 web.xml,注意位置
1.4、配置服务器运行环境
2、部署 MyBatis
2.1、添加 mybatis 依赖
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- mysql驱动 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
2.2、创建 mybatis 配置文件 mybatis-config.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>
</configuration>
3、部署 spring、springMVC
3.1、添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
3.2、创建 spring 配置文件(多配置文件分开配置)
3.2.1、spring-context.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: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/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 声明使用注解配置 -->
<context:annotation-config></context:annotation-config>
<!-- 声明Spring工厂注解的扫描范围 -->
<context:component-scan base-package=""></context:component-scan>
</beans>
3.2.2、spring-mvc.xml:进行 mvc 相关的配置,如 静态资源配置、拦截器配置
<?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:mvc="http://www.springframework.org/schema/mvc"
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">
<!--声明mvc使用注解配置-->
<mvc:annotation-driven/>
</beans>
3.2.3、spring-mybatis.xml:进行 spring 与 mybatis 整合相关的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
3.3、配置 SpringMVC 前端控制器
在 web.xml 进行配置
<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:spring-*.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>
4、整合配置
4.1、导入 mybatis-spring 依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
4.2、配置 druid 连接池
4.2.1、添加 druid 依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
4.2.2、创建 druid.properties
druid.driver=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&characterEncoding=utf8
druid.username=root
druid.password=zyj123
#连接池参数
#init:初始连接数 minIdle:最小连接数 maxActive:最大连接数 timeout:超时时间
druid.pool.init=1
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000
4.2.3、在 spring-mybatis.xml 中配置
<context:property-placeholder location="classpath:druid.properties"/>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="initialSize" value="${druid.pool.init}"/>
<property name="minIdle" value="${druid.pool.minIdle}"/>
<property name="maxActive" value="${druid.pool.maxActive}"/>
<property name="maxWait" value="${druid.pool.timeout}"/>
</bean>
4.3、配置 SqlSessionFactory
在 spring-mybatis.xml 中配置
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"></property>
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
<property name="typeAliasesPackage" value="com.zyj.pojo"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
4.4、配置 MapperScnnerConfigurer
在 spring-mybatis.xml 中配置
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.zyj.dao"></property>
</bean>
4.5、整合配置 AOP
使用Spring提供的事务管理完成DAO操作的事务管理
基于注解的事务管理配置
将Spring提供的事务管理切面类配置到Spring容器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
5、测试
5.1、准备工作
① 创建数据表
② 创建 User 实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private Integer userId;
private String username;
private String userPwd;
private String userRealName;
private String userImg;
}
5.2、完成 User 类的查询操作
① 在 dao 包下创建接口
public interface UserDao {
public User queryUserByName(String name);
}
② 在 mappers 目录下创建映射文件 UserMapper.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.zyj.dao.UserDao">
<resultMap id="userMap" type="User">
<id column="user_id" property="userId"></id>
<result column="user_name" property="userName"></result>
<result column="user_pwd" property="userPwd"></result>
<result column="user_realname" property="userRealName"></result>
<result column="user_img" property="userImg"></result>
</resultMap>
<!-- public User queryUserByName(@Param("name") String name); -->
<select id="queryUserByName" resultMap="userMap">
select * from users where user_name = #{name}
</select>
</mapper>
5.3、使用 junit 进行测试
① 导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
② 创建测试类及测试方法
public class UserDaoTest {
@Resource
private UserDao userDao;
@Test
public void testQueryUserByName(){
User user = userDao.queryUserByName("Java");
System.out.println(user);
}
}