使用Mapper代理开发原因
1.程序员只需要写mapper接口和mapper.xml映射文件,Mybatis可以自动生成mapper接口实现类代理对象
2.只不过程序员在编写mapper接口时,需要遵循一些开发规范
- mapper.java接口名 跟 mapper.xml映射文件名相同并且在同一包下
- 在mapper.xml映射文件中,namespace=“mapper接口地址”–完全包名.mapper接口名
- mapper.java接口中的方法名和mapper.xml中statement的id值一致
- mapper.java接口中的方法输入参数类型 和 mapper.xml中statement的parameterType指定的类型一致
- mapper.java接口中的方法返回值类型 和 mapper.xml中statement的resultType指定的类型一致
- SqlMapConfig.xml 配置文件中 —由于使用mapper代理方式,改为加载mapper接口
目录结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>MyBatisDao</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.8</version>
</dependency>
<!--junit测试包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--日志包,方便查看动态sql语句 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</project>
DeptMapper
public interface DeptMapper {
List<Dept> SelectAllDept() throws Exception;
int InsertDept(Dept dept) throws Exception;
int UpdateDept(Dept dept) throws Exception;
int DelDept(int id) throws Exception;
}
Dept
public class Dept {
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Dept() {
}
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
}
initLogRecord
public class initLogRecord {
public static void initLog() {
FileInputStream fileInputStream = null;
try {
Properties properties = new Properties();
fileInputStream = new FileInputStream("src/main/resources/log4j.properites");
properties.load(fileInputStream);
PropertyConfigurator.configure(properties);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
DeptMapper
<?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">
<!--namespace填写mapper接口的全限定名-->
<mapper namespace="com.msb.mapper.DeptMapper">
<!--id要和方法名相同-->
<!--方法返回值类型 和 mapper.xml中statement的resultType指定的类型一致-->
<select id="SelectAllDept" resultType="dept">
select * from dept
</select>
<!--方法输入参数类型 和 mapper.xml中statement的parameterType指定的类型一致-->
<insert id="InsertDept" parameterType="dept">
insert into dept(deptno,dname,loc) values(#{deptno},#{dname},#{loc})
</insert>
<delete id="DelDept" parameterType="int">
delete from dept where deptno = #{deptno}
</delete>
<update id="UpdateDept" parameterType="dept">
update dept set dname = #{dname} where deptno = #{deptno}
</update>
</mapper>
JDBC.properites
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.220.130:3306/test_project?useUnicode=true&characterEncoding=UTF-8
username=root
password=qwert123
log4j.properites
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
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>
<properties resource="JDBC.properites"/>
<typeAliases>
<!--设置别名,name为当前实体类的所在位置,映射文件resultType别名为当前类名首字母小写后的名字-->
<package name="com.msb.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url"
value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--接口所在的包名-->
<package name="com.msb.mapper"/>
</mappers>
</configuration>
MybatisTest
public class MybatisTest {
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
initLogRecord.initLog();
InputStream resource = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
factory = builder.build(resource);
}
@Test
public void test() throws Exception {
SqlSession sqlSession = factory.openSession();
//传入参数为想获得接口的实现类
DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> list = mapper.SelectAllDept();
for (Dept dept: list) {
System.out.println(dept.getDeptno()+";"+dept.getDname()+";"+dept.getLoc());
}
}
}
运行结果