作者:dxj20101118 | 来源:互联网 | 2023-09-24 17:53
篇首语:本文由编程笔记#小编为大家整理,主要介绍了Mybatis中Mapper代理形式开发与spring整合相关的知识,希望对你有一定的参考价值。
1.导入jar包
2.分包
- cogfig:存放配置文件
- mapper:存放映射与接口
- pojo:存放实体类
- test:测试代码
3.编写配置文件
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>
<package name="com.pojo"/>
typeAliases>
configuration>
log4j.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
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis01?characterEncoding=utf-8
jdbc.username=root
jdbc.password=toor
applicationContext.xml
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">
beans>
4.编写实体类、接口、映射
User.java
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..............................
}
UserMapper.java
public interface UserMapper {
/**
* 通过ID查User
* @return
*/
public User findUserById(Integer id);
}
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.mapper.UserMapper">
<select id="findUserById" parameterType="Int" resultType="User">
select * from user where id = #{id}
select>
mapper>
5.编写applicationContext.xml
配置数据库连接池
<context:property-placeholder location="classpath:com/config/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工厂
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:com/config/SqlMapConfig.xml"/>
bean>
配置mapper代理对象
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
bean>
6.测试
public class Demo1 {
@Test
public void m01(){
//加载配置文件
ClassPathXmlApplicationContext cOntext= new ClassPathXmlApplicationContext("classpath:com/config/applicationContext.xml");
//获取mapper
UserMapper mapper = (UserMapper) context.getBean("userMapper");
//执行SQL
User user = mapper.findUserById(10);
System.out.println(user);
}
}
Mapper代理的配置方式二(推荐推荐推荐)
第一种配置方式过于繁琐,有多少个mapper就需要配置多少次
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"/>
bean>
修改获取bean的方式
UserMapper mapper = (UserMapper) context.getBean(UserMapper.class);