Mybatis整合Spring之Mapper动态代理开发

步骤

1.创建一个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>
	<!-- 设置别名 -->
	<typeAliases>
		<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
		<package name="com.itheima.mybatis.pojo" />
	</typeAliases>
	
</configuration>

2.创建一个applicationContext.xml文件

加载配置文件db.properties  
配置数据库链接池
配置sqlSessionFactory 工厂
	配置mybatis 的核心文件
	配置数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

   <!-- 加载配置文件 -->
   <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>

	<!-- 配置Mybatis的工厂 SqlSessionFactory -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>

3.创建pojo类(User)

public class User implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private String username;
	private String sex;
	private Date birthday;
	private String address;
	
set\get....

4.实现UserMapper接口

public interface UserMapper {
	public User selectUserById(Integer id);
}

5.实现UserMapper.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.itheima.mybatis.mapper.UserMapper">
	<select id="selectUserById" parameterType="Integer" resultType="User">
		select * from user where id = #{id}
	</select>
</mapper>

6.在sqlMapConfig.xml中加载UserMapper.xml

<mappers>
	<package name="com.itheima.mybatis.mapper"/>
</mappers>

7.在applicationContext.xml中配置mapper

  • 法一:
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
	<property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"/>
</bean>
  • 法二:扫描(企业中经常使用)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 指定基本包 -->
	<property name="basePackage" value="com.itheima.mybatis.mapper"/>
</bean>	

8.测试

public void testMapper() {

	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
	//法一
	UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
	User selectUserById = userMapper.selectUserById(10);
	System.out.println(selectUserById);
	
	//法二
	UserMapper mapper = ac.getBean(UserMapper.class);
	User selectUserById2 = mapper.selectUserById(10);
	System.out.println(selectUserById2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值