spring整合mybatis框架

时间:2023-03-08 18:10:42
spring整合mybatis框架

1.导入jar包

2.配置文件

a.    applicationContext.xml文件

<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 配置数据源dataSource -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean> <!-- 原始dao -->
<bean id="userDao" class="com.baidu.dao.imp.UserDaoImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- mapper代理配置 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 指定mapper接口 -->
<property name="mapperInterface" value="com.baidu.dao.UserDao"/>
<!-- 注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
 <!-- 使用mapper扫描器创建mapper代理对象
扫描器把自动将包下边的mapper扫描出来创建代理对象在spring容器注册,bean的id为类名(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包路径,如果要扫描多个包,中间使用半角逗号分隔
注意:如果使用扫描器,不需要在sqlMapConfig.xml中加载mapper,要将mapper.xml和mapper.java放在同一个目录且同名
-->
<property name="basePackage" value="com.baidu.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

  

b.    SqlMapperConfig.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>
<typeAliases>
<typeAlias type="com.baidu.pojo.User" alias="u" />
</typeAliases>
<!-- 加载mapper.xml -->
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
</configuration>

c. XxxMaper.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.baidu.dao.UserDao">
<!-- 查询用户通过id -->
<select id="findById" parameterType="u" resultMap="u">
SELECT id,username,sex,birthday,address
FROM USER
<where>
<if test="id!=null and id!=''">
and id = #{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
</mapper>

d.   db.propertis

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123

3.相关类

spring整合mybatis框架

a.UserDao.java

package com.baidu.dao;

import com.baidu.pojo.User;

public interface UserDao {
public User findById(User u);
}

b.UserDaoImp.java

package com.baidu.dao.imp;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport; import com.baidu.dao.UserDao;
import com.baidu.pojo.User; public class UserDaoImp extends SqlSessionDaoSupport implements UserDao{ @Override
public User findById(User u) {
//获取session
// Sqlsession定义为局部变量
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("com.baidu.dao.UserDao.findById", u);
return user;
}
}

  继承sqlSessionDaoSupport的原因是

spring整合mybatis框架

c.User.java

public class User implements Serializable {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
}

d.MyBatisTest.java

public class MyBatisTest {
private ApplicationContext applicationContext;
@Before
public void setup(){
//加载spring配置文件
//取出spring的容器
applicationContext =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception { //从容器中取出dao
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//调用dao的方法
User u=new User();
u.setUsername("fan");
User user = userDao.findById(u);
System.out.println(user); } }