MyBatis与Spring的整合

整合环境搭建

准备所需要的jar包

在这里插入图具体jar包片描述具体jar包可以自己去找

编写配置文件
1.db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5
2.Spring的配置文件

​ applicationContext.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"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<context:component-scan
		base-package="com.software.cms" />

	<context:property-placeholder
		location="classpath:db.properties" />

	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp2.BasicDataSource">
		<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="maxTotal" value="${jdbc.maxTotal}"></property>
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<property name="initialSize" value="${jdbc.initialSize}"></property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 开启事务注解 -->
	<tx:annotation-driven
		transaction-manager="transactionManager" />
	<!-- 配置MyBatis工厂 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 指定核心配置文件位置 -->
		<property name="configLocation"
			value="classpath:mybatis-config.xml"></property>
	</bean>
	
	<!-- 注入sqlsessionFactory -->
	<bean id="customerDao" class="com.software.cms.Dao.Impl.CustomerDaoImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	
	<!-- Mapper代理开发,基于MapperFactoryBean 
	<bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.software.cms.mapper.CustomerMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- 自动扫描Mapper包下所有的接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.software.cms.mapper"></property>
	</bean>
</beans>
3.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>
	<!-- 便于在开发环境和生产环境中切换 -->
	<properties resource="db.properties"></properties>
	<settings>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 积极加载改为消极加载 -->
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	<!-- 给类型添加别名,默认是类的名称首字母小写,大小写都可以 -->
	<typeAliases >
		<package name="com.software.cms.bean"/>
	</typeAliases>
	
	<environments default="delevopment">
		<environment id="delevopment">
			<transactionManager type="JDBC"></transactionManager>
			<!-- 使用数据源连接池 -->
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driver}"/>
				<property name="url" value="${jdbc.url}"/>
				<property name="username" value="${jdbc.username}"/>
				<property name="password" value="${jdbc.password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 由Spring自动扫描 
	<mappers>
		<mapper resource="com/software/cms/mapper/CustomerMapper.xml"/>
	</mappers> -->
</configuration>
传统DAO方式的开发整合
1.实现持久化层

​ Customer.java

/**
 * 用户类
 * 
 * @author Dell
 *
 */
public class Customer {
	private int id;
	private String username;
	private String job;
	private String phone;

	public Customer(int id, String username, String jobs, String phone) {
		super();
		this.id = id;
		this.username = username;
		this.job = jobs;
		this.phone = phone;
	}

	public Customer() {
		super();
	}

	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 getJobs() {
		return job;
	}

	public void setJobs(String jobs) {
		this.job = jobs;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + ", username=" + username + ", job=" + job + ", phone=" + phone + "]";
	}

}
2.创建映射文件CustomerMapper.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.software.cms.mapper.CustomerMapper">
	<select id="findCustomerById" parameterType="Integer" resultType="customer">
		select * from t_customer where id=#{id}
	</select>
</mapper>
3.实现DAO层
(1)创建接口CustomerDao
public interface CustomerDao {
	public Customer findCustomerById(Integer id);
}
(2)创建接口CustomerDao的实现类CustomerDaoImpl
public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {

	@Override
	public Customer findCustomerById(Integer id) {
		// TODO Auto-generated method stub
		return this.getSqlSession().selectOne("findCustomerById", id);
	}

}
4.整合测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class DaoTest {
	
	@Autowired
	private CustomerDao customerDao;
	
	@Test
	public void testFindCustomerById() {
		Customer customer = customerDao.findCustomerById(4);
		System.out.println(customer);
	}

}
Mapper接口方式的开发整合

虽然使用Mapper接口变成的方式很简单,但是使用时还是需要遵循以下规范。

(1)Mapper接口的名称和对应的Mapper.xml映射文件的名称必须一致。

(2)Mapper.xml文件正的namespace与Mapper接口的类路径相同(即接口文件和映射文件需要放在同一个包里)。

(3)Mapper接口中的方法名和Mapper.xml中定义的每个执行语句的id相同。

(4)Mapper接口中方法的输入参数类型要和Mapper.xml中定义的每个sql的parameterType的类型相同。

(5)Mapper接口中方法的输出参数类型要和Mapper.xml中定义的每个sql的resultType的类型相同。

1.在mapper包下创建一个CustomerMapper.java

CustomerMapper.java

public interface CustomerMapper {
	public Customer findCustomerById(Integer id);
}

CustomerMapper.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.software.cms.mapper.CustomerMapper">
	<select id="findCustomerById" parameterType="Integer" resultType="customer">
		select * from t_customer where id=#{id}
	</select>
</mapper>

其余配置都已添加在上方Spring配置文件中

2.整合测试
@Autowired
	private CustomerMapper customerMapper;
	
	@Test
	public void testFindCustomerByIdMapper() {
		Customer customer= customerMapper.findCustomerById(3);
		System.out.println(customer);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值