小铃铛 2019.10.28
那今天什么是Mybatis???mybatis是一个持久层框架,apache基金会下的一个顶级项目(开源的),是一个半自动化的ORMapping
<configuration>
<!-- 导入db.properties文件 -->
<properties resource="db.properties" />
<!-- 批量修改别名 (cn.lanqia.entity) -->
<typeAliases>
<package name="cn.lanqiao.entity"/>
</typeAliases>
<!-- 数据库连接信息配制 -->
<environments default="developer">
<environment id="developer">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environment>
</environments>
<!-- 导入映射 文件 -->
<mappers>
<package name="cn.lanqiao.mapper"/>
</mappers>
</configuration>
<!-- 编写db.properties文件 -->
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatisdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
username=root
password=123456
在工作中,mybatis的开发一般使用mapper代理模式
<mapper namespace="mapper接口全路径">
<cache/>
<select id="" resultType="" resultMap="" paramType="" userCache="false"/>
<update/>
<delete />
<insert />
<resultmap />
<sql />
<include />
<!-- 动态 SQL -->
</mapper>
//测试代码,,,看大致即可
package cn.lanqiao.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import cn.lanqiao.entity.Department;
import cn.lanqiao.entity.vo.EmpVo;
import cn.lanqiao.mapper.DepartmentMapper;
import cn.lanqiao.mapper.EmpMapper;
public class departmentTest {
private static SqlSessionFactory factory=null;
static {
String resource="mybatis-config.xml";
try {
InputStream in=Resources.getResourceAsStream(resource);
factory=new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {e.printStackTrace();}
}
@Test
public void testGet() {
SqlSession session=factory.openSession();
DepartmentMapper mapper=session.getMapper(DepartmentMapper.class);
Department dept=mapper.get(10);
System.out.println(dept);
}
@Test
public void testgetbyid() {
SqlSession session=factory.openSession();
EmpMapper mapper=session.getMapper(EmpMapper.class);
EmpVo empvo=mapper.getEmpByEmpno(4);
System.out.println(empvo);
System.out.println(empvo.getDname());
}
}
//log4j.properties
# 日志输出到控制台
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
# log4j.logger.org.mybatis.example.BlogMapper=TRACE
# 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
# 日志输出到文件;
4 、基于xml的2种开发模式【重点】
1、传统的dao模式【了解】
2、mapper代理模式【重点掌握】
只需要用户编写接口,实现类有mybatis自动生成;使用此模式开发,
需要遵守mapper代理模式规范(4条)
//EmpMapper.java
package cn.lanqiao.mapper;
import cn.lanqiao.entity.vo.EmpVo;
public interface EmpMapper {
public EmpVo getEmpByEmpno(int empno);
}
//xml代码 EmpMapper.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="cn.lanqiao.mapper.EmpMapper">
<select id="getEmpByEmpno" parameterType="int" resultType="cn.lanqiao.entity.vo.EmpVo">
select
e.*,d.dname
from emp e,dept d
where e.deptno=d.dno and empno=#{empno}
</select>
</mapper>
mapper代理模式有4点规范
1、映射文件根节点的namespace名称为mapper接口全路径
2、变迁的id与mapper接口的方法名必须一样
3、标签的返回值类型与mapper接口中方法的返回值类型一致
4、标签总的参数类型与mapper接口中方法的参数类型一致
mapper接口命名规范 xxxMapper
当数据库设计的属性和java设计的属性不是一样的时候,就需要手动映射
package cn.lanqiao.mapper;
import cn.lanqiao.entity.Department;
public interface DepartmentMapper {
public Department get(int deptno);
}
<?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="cn.lanqiao.mapper.DepartmentMapper">
<resultMap type="department" id="deptresultmap">
<!-- id对应的是主键 手动映射-->
<id column="dno" property="deptno"/>
<result column="dname" property="deptname"/>
</resultMap>
<select id="get" parameterType="int" resultMap="deptresultmap">
select * from dept where dno=#{dno};
</select>
</mapper>
在mapper文件中写
大致是这样的
<select id="search" resultType="emp" parameterType="employeevo">
select * from emp
<where>
<if test="deptno!=null"> //查询参数是java中的参数,如果不为空
and deptno=#{deptno}
</if>
<if test="esal!=null">
and esal=#{esal}
</if>
</where>
</select>