文章目录
- 代理
- xml方式
- UserMapper.java
- User.java
- db.properties
- SqlMapConfig.xml
- UserMapper.xml
- UserMapperTest.java
- pom.xml
代理
此处使用的是JDK的动态代理方式,延迟加载使用的cglib动态代理方式
代理分为静态代理和动态代理。此处先不说静态代理,因为Mybatis中使用的代理方式是动态代理。
动态代理分为两种方式:
基于JDK的动态代理–针对有接口的类进行动态代理
基于CGLIB的动态代理–通过子类继承父类的方式去进行代理。
xml方式
开发方式
只需要开发Mapper接口(dao接口)和Mapper映射文件,不需要编写实现类。
开发规范
Mapper接口开发方式需要遵循以下规范:
1、 Mapper接口的类路径与Mapper.xml文件中的namespace相同。
2、 Mapper接口方法名称和Mapper.xml中定义的每个statement的id相同。
3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同。
4、 Mapper接口方法的返回值类型和mapper.xml中定义的每个sql的resultType的类型相同。
UserMapper.java
package com.xd.mybatisdemo.mapper;import com.xd.mybatisdemo.pojo.User;public interface UserMapper {public User findUserById(int id) throws Exception;}
User.java
package com.xd.mybatisdemo.pojo;import java.util.Date;public class User {private int id;private String username;private Date birthday;private String sex;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 Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}
}
db.properties
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/mybatisdemo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
db.username=root
db.password=root
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="db.properties"></properties><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="${db.driver}" /><property name="url" value="${db.url}" /><property name="username" value="${db.username}" /><property name="password" value="${db.password}" /></dataSource></environment></environments><mappers><mapper resource="UserMapper.xml" /></mappers>
</configuration>
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xd.mybatisdemo.mapper.UserMapper"><!-- 根据id获取用户信息 --><select id="findUserById" parameterType="int"resultType="com.xd.mybatisdemo.pojo.User">select * from user where id = #{id}</select></mapper>
UserMapperTest.java
package com.xd.mybatisdemo;import com.xd.mybatisdemo.mapper.UserMapper;
import com.xd.mybatisdemo.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;import java.io.InputStream;public class UserMapperTest {private SqlSessionFactory sqlSessionFactory;@Beforepublic void setUp() throws Exception {
//mybatis配置文件String resource = "SqlMapConfig.xml";InputStream inputStream = Resources.getResourceAsStream(resource);
//使用SqlSessionFactoryBuilder创建sessionFactorysqlSessionFactory = newSqlSessionFactoryBuilder().build(inputStream);}@Testpublic void testFindUserById() throws Exception {
//获取sessionSqlSession session = sqlSessionFactory.openSession();
//获取mapper接口的代理对象UserMapper userMapper = session.getMapper(UserMapper.class);
//调用代理对象方法User user = userMapper.findUserById(1);System.out.println(user.getUsername());
//关闭sessionsession.close();}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>mybatisdemo</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>mybatisdemo</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- mybatis依赖 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!-- mysql依赖 -->
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>5.1.35</version>-->
<!-- </dependency>--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.32</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>