Mybatis和Spring整合

1、Mybatis和Spring框架整合

1.1、导入所需的包

我所需要的包是
在这里插入图片描述

1.2、创建Mybatis主配置文件sqlMapConfig.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>
  <!-- 类型别名,推荐使用package包的形式 -->
  <typeAliases>
  	<!-- 扫描该包主包及子包下的所有类都起别名,别名为类名,而且大小写不敏感,推荐使用小写 -->
  	<package name="com.xiezhenyu.bean"/>
  </typeAliases>
</configuration>
1.3、创建Spring主配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://www.springframework.org/schema/beans" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:util="http://www.springframework.org/schema/util" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd 
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
	<!-- 读取db.properties -->
	<context:property-placeholder location="db.properties"/>
	<!-- 配置c3p0连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 配置MyBatis sqlSessionFactory -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 告诉spring mybatis的核心配置文件 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
	</bean>
</beans>
1.4、创建log4j.properties和db.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=Asia/Shanghai
jdbc.user=root
jdbc.password=p123456
1.5、测试
public static void main(String[] args) {
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	SqlSessionFactoryBean bean = ac.getBean(SqlSessionFactoryBean.class);
	System.out.println(bean);
}
1.6、结果
org.mybatis.spring.SqlSessionFactoryBean@708f5957

输出以上结果代表已经成功!

2、Dao式开发

在传统dao层上使用MyBatis和spring整合开发

2.1、创建实体类
package com.xiezhenyu.bean;
import java.util.Date;
public class User {
	private Integer u_id;
	private String u_username;
	private String u_password;
	private String u_sex;
	private Date u_createTime;
	private Integer u_cid;
	public Integer getU_id() {
		return u_id;
	}
	public void setU_id(Integer u_id) {
		this.u_id = u_id;
	}
	public String getU_username() {
		return u_username;
	}
	public void setU_username(String u_username) {
		this.u_username = u_username;
	}
	public String getU_password() {
		return u_password;
	}
	public void setU_password(String u_password) {
		this.u_password = u_password;
	}
	public String getU_sex() {
		return u_sex;
	}
	public void setU_sex(String u_sex) {
		this.u_sex = u_sex;
	}
	public Date getU_createTime() {
		return u_createTime;
	}
	public void setU_createTime(Date u_createTime) {
		this.u_createTime = u_createTime;
	}
	public Integer getU_cid() {
		return u_cid;
	}
	public void setU_cid(Integer u_cid) {
		this.u_cid = u_cid;
	}
	@Override
	public String toString() {
		return "User [u_id=" + u_id + ", u_username=" + u_username + ", u_password=" + u_password + ", u_sex=" + u_sex
				+ ", u_createTime=" + u_createTime + ", u_cid=" + u_cid + "]";
	}
}
2.2、书写Dao和DaoImpl

dao

package com.xiezhenyu.dao;
import com.xiezhenyu.bean.User;
public interface UserDao {
	//根据id查询用户
	public User getUserById(Integer id);
}

  daoImpl继承SqlSessionDaoSupport,可以通过父类的getSqlSession()方法直接获得session,而父类中的sqlSessionFactory可以通过spring注入。

package com.xiezhenyu.dao;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.xiezhenyu.bean.User;
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
	@Override
	public User getUserById(Integer id) {
		//通过父类的方法直接获得session
		SqlSession session = getSqlSession();
		return session.selectOne("UserMapper.selectUserById",id);
	}
}
2.3、配置spring配置文件applicationContext.xml

将工厂注入dao的父类 sqlSessionFactory属性中

<!-- 将工厂注入dao的父类 sqlSessionFactory属性中 -->
<bean id="userDaoImpl" class="com.xiezhenyu.dao.UserDaoImpl">
	<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean>
2.4、配置mybatis配置文件
<mappers>
	<mapper resource="com/xiezhenyu/mapper/UserMapper.xml"/>
</mappers>
2.5、测试
@Test
public void DaoTest() {
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	UserDaoImpl userDao = ac.getBean(UserDaoImpl.class);
	User user = userDao.getUserById(1);
	System.out.println(user);
}
2.6、结果
User [u_id=1, u_username=老王, u_password=123, u_sex=1, u_createTime=null, u_cid=1]

3、Mapper动态代理开发

3.1、创建实体类

该过程和上面dao层开发的实体类一致,就不给出代码。

3.2、创建UserMapper接口,修改UserMapper.xml

将UserMapper.xml的namespace修改为UserMapper接口的路径

public interface UserMapper {
	public User selectUserById(Integer id);
}
<mapper namespace="com.xiezhenyu.mapper.UserMapper">
	<!-- 查找用户 -->
	<select id="selectUserById" parameterType="Integer" resultType="user">
		select * from user where u_id = #{id}
	</select>
</mapper>
3.3、让sqlMapConfig.xml以包的形式扫描mapper.xml
<mappers>
	<package name="com.xiezhenyu.mapper"/>
</mappers>
3.4、配置spring配置文件applicationContext.xml

  将MapperFactoryBean交给Spring管理。MapperFactoryBean中拥有属性mapperInterface可以配置mapper的接口,而MapperFactoryBean是继承SqlSessionDaoSupport,使用可以注入sqlSessionFactory。

<!-- Mapper动态代理开发 -->
<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<!-- 注入sqlSessionFactory -->
	<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
	<!-- 配置接口 -->
	<property name="mapperInterface" value="com.xiezhenyu.mapper.UserMapper"/>
</bean>
3.5、编写测试方法

获取mapper的方法可以用传递applicationContext.xml中id的方法,也可以直接传递接口。

@Test
public void Test1() {
	//获取主配置文件
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	//UserMapper mapper = (UserMapper)ac.getBean("UserMapper");
	UserMapper mapper = ac.getBean(UserMapper.class);
	User user = mapper.selectUserById(1);
	System.out.println(user);
}
3.6、结果
User [u_id=1, u_username=老王, u_password=123, u_sex=1, u_createTime=null, u_cid=1]

4、Mapper动态扫描开发(推荐使用)

  在mapper动态扫描开发中只需要配置以下即可,他会自动扫描某个包下的全部mapper,它不用手动注入sqlSessionFactory。

<!-- mapper动态扫描开发 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.xiezhenyu.mapper"/>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢以轩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值