简化 MyBatis
1. 导包
导 入 mybatis 所 有 jar 和 spring 基 本包,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合 mybatis 的包等

2. 配置 web.xml
- 封装了一个监听器,帮助加载 Spring 的配置文件爱
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<context-param> <param-name>contextConfigLocation</param-name>
<!-- spring 配置文件 -->
<param-value>classpath:applicationContext.xml</para m-value>
</context-param>
<!-- 封装了一使用 Spring 简化 MyBatis
1. 导 入 mybatis 所 有 jar 和 spring 基 本
包,spring-jdbc,spring-tx,spring-aop,spring-web,spring 整合 mybatis 的包
等
2. 先配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 上下文参数 -->
<context-param> <param-name>contextConfigLocation</param-name>
<!-- spring 配置文件 -->
<param-value>classpath:applicationContext.xml</para m-value>
</context-param>
<!--1-->
<listener>
<listener-class>org.springframework.web.context.Con textLoaderListener
</listener-class>
</listener> </web-app>
3. 编写 spring 配置文件 applicationContext.xml
学习mybatis需要配置全局文件,用sping提供的类对应mybatis的xml文件
-
其中dataSource标签中的内容是连接数据库的相关信息,用数据源封装类代替,需要导入spring-jdbc.jar
-
创建 SqlSessionFactory 对象,导入mybatis-spring,jar
-
数据库连接信息来源于 dataSource (属性名-类型),类型是对象,用ref进行注入
前三步骤完成就产生了factory对象 -
声明事务在下一篇分享
-
用一个类来代替mappers标签,依然在mybatis-spring,jar。扫描器相当于 mybatis.xml 中 mappers 下 package 标 签,扫描 com.youdian.mapper 包后会给对应接口创建对象
-
用basePackage全局属性来给进行包扫描,类型是String,用values赋值
-
让2和7之间和factory 产生关系
以上是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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--1-->
<bean id="dataSouce" class="org.springframework.jdbc.
datasource.DriverMana gerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/sum"></property> <property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--2-->
<bean id="factory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3-->
<property name="dataSource" ref="dataSouce"></property>
</bean>
<!--5-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigu rer">
<!--6-->
<property name="basePackage" value="com.youdian.mapper"></property>
<!--7-->
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
4.代码实现
- 正常编写 pojo
加入set()和get()方法
public class Airport {
private int id;
private String portName;
private String cityName;
}
- 新建一个接口mapper
查询全部。可以用注解,也可以用配置文件
public interface AirportMapper {
@Select("select * from airport")
List<Airport> selAll();
}
- 新建一个测试类test
①编译后的applicationContext.xml在classes中,而ClassPathXmlApplicationContext 默认去classes文件夹根目录开始寻找,因此不需要写入全路径
②需要导入一个事务的相关包spring-tx.jar,spring-aop.jar
public class Test {
public static void main(String[] args) {
//创建spring容器
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
String[] names = ac.getBeanDefinitionNames();
for (String string : names) {
System.out.println(string);
}
}
}
AirportServiceImpl bean = ac.getBean("airportService",AirportServiceImpl.class);
List<Airport> list = bean.show();
System.out.println(list);
- 新建service
接口:查询全部
public interface AirportService {
List<Airport> show();
}
实现类:
public class AirportServiceImpl implements AirportService {
private AirportMapper airportMapper;
//加入set和get方法
public AirportMapper getAirportMapper() {
return airportMapper;
}
public void setAirportMapper(AirportMapper airportMapper) {
this.airportMapper = airportMapper;
}
@Override
public List<Airport> show() {
return airportMapper.selAll();
}
}
把该类配置到spring后,spring的MapperScannerConfigurer会把类的对象创建好,需要配置到applicationContext.xml中。到此AirportMapper就会被实例化,相当于session.getMapper()
<!-- 由 spring 管理 service 实现类 -->
<bean id="airportService"
class="com.youdian.service.impl.AirportServiceImpl">
<property name="airportMapper" ref="airportMapper"></property>
</bean>
</beans>
在测试类test中进行测试:
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
AirportServiceImpl bean =
ac.getBean("airportService",AirportServiceImpl.class);
List<Airport> list = bean.show();
System.out.println(list);
总结:
用四个标签替换了mybatis的全局配置文件,filter类,util类
- 正常编写pojo
- 编写 mapper 包下时必须使用接口绑定方案或注解方案(必须
有接口) - 正常编写service接口和实现类
需要在service实现类中声明Mapper接口对象,并生成set和get方法 - spring无法管理servlet
新建一个com.youdian.servlet
①在未简化之前,是需要自己在servlet.java文件中进行new对象,而现在类被spring容器管理了,所以需要从spring容器中把对象取出来
②spring提供在TomCat启动时,自动加载配置文件web.xml,在web.xml中配置。面对整体学习思路,把web.xml的配置放在全局配置文件之前
@WebServlet("/airport")
public class AirportServlet extends HttpServlet{
private AirportService airportService;
@Override
public void init() throws ServletException {
//对service实例化
//ApplicationContext ac = new
//ClassPathXmlApplicationContext("applicationContext.xml");
//airportService=ac.getBean("airportService",AirportServiceImpl.class);
//spring和web整合后所有信息都存放在webApplicationContext
ApplicationContext ac =
WebApplicationContextUtils.getRequiredWebApplicationContext
(getServletContext());
airportService=ac.getBean("airportService",AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("list", airportService.show());
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}

本文详细介绍如何将MyBatis与Spring框架整合,包括导入所需jar包、配置web.xml、编写Spring配置文件applicationContext.xml、代码实现等步骤,实现通过Spring管理MyBatis的Mapper接口。
1765

被折叠的 条评论
为什么被折叠?



