mybatis快速入门(七)-spring-mybatis整合

时间:2024-07-24 15:04:50

今天写写spring-mybatis整合吧,先写个原始dao类的整合,下一节在写个动态代理的整合,我就不写太详细了,因为前面的章节基本上都有了,我直接就一口气都写出来需要那些文件然后在直接贴代码,首先创建一个java项目,。

首先导入jar包,jar地址下载

mybatis快速入门(七)-spring-mybatis整合

目录结构

mybatis快速入门(七)-spring-mybatis整合

1.db.properties数据连接池

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

2.applicationContext.xml,spring的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: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 "> <!-- 数据源配置 dbcp -->
<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>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- 创建sqlsessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis/SqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="cn.my.sm.dao.UserDao" >
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>

3.SqlMapConfig.xml:mybatis的映射文件

<?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>
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
</configuration>

4.User.xml:实体类的映射文件,为了简便我就只写一个findById方法的映射了

<?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="cn.my.sm.mapper.UserDaoMapper">
<select id="findById" parameterType="int" resultType="cn.my.sm.po.User">
select * from user where id=#{id}
</select>
</mapper>

5.创建User.java实体类

package cn.my.sm.po;

import java.util.Date;

public class User {

    private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + "]";
}
}

6.创建UserDao.java数据访问类

package cn.my.sm.dao;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport; import cn.my.sm.po.User;
//原始dao层需要继承SqlSessionDaoSupport类,或者自己声明一个sqlsessionfactory对象,添加setget方法,然后自己根据sqlsessionfactory工厂获取sqlsession
public class UserDao extends SqlSessionDaoSupport {
public User findById(int id){
SqlSession session = this.getSqlSession();
User user = session.selectOne("cn.my.sm.mapper.UserDaoMapper.findById", id);
return user;
}
}

7.创建测试类

package cn.my.sm.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.my.sm.dao.UserDao;
import cn.my.sm.po.User; @SuppressWarnings("resource")
public class TestUser {
ApplicationContext context;
@Before
public void setUp(){
context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
}
@Test
public void testFindById(){
UserDao userdao = (UserDao) context.getBean("userDao");
User user = userdao.findById(1);
System.out.println(user);
}
}

允许结果:

User [id=1, username=王五, sex=2, birthday=null, address=null]

这样就原始整合完了,今天写得比较草率,如果大家有疑问的可以加群号581591235或者留言。也可以加微信群

mybatis快速入门(七)-spring-mybatis整合