目录
1、junit的使用步骤
1.1、在pom.xml文件中添加Junit依赖
可复制版:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
1.2、生成测试类
找到要生成测试类的当前位置,一般为类所对应的接口文件,可直接在接口文件中按快捷键ctrl+shift+T出现如下内容
选择要生成测试的方法
生成测试类
public class StudentMapper213Test {
@Test
public void selectStudentById() {
String resource = "mybatis-config.xml";
InputStream stream = null;
try {
stream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
//测试接口
Student213 student213 = studentMapper213.selectStudentById(1);
System.out.println(student213);
}
@Test
public void deleteStudentById() {
}
}
2、Junit介绍
Junit是用于编写和运行可重复的自动化测试的开源框架,适用于测试整个对象或
对象的一部分,交互中的一个方法或者是一些方法,对象之间的交互
2.1、常用的注解:
@Before
当写测试方法时,出现在一些方法之前,会创建相同的对象,可以将其放入before中操作
使用before注解的方法一般是public void 方法名 ,就会在@test方法之前被执行
@Test
tese注解的public void方法将被作为测试用例
Junit每次都会创建一个新的测试实例,然后调用@test注解的方法
@Test
public void deleteStudentById() {
}
@After
如果分配了额外的资源,在测试完成之后需要关闭资源,
使用after注解给定一个public void 方法在test注解的方法执行完成之后在执行
SqlSessionFactory sqlSessionFactory = null;
@Before
public void Before(){
String resource = "mybatis-config.xml";
InputStream stream = null;
try {
stream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
System.out.println("before方法执行完成");
}
@Test
public void selectStudentById() {
System.out.println("测试select方法");
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
//测试接口
Student213 student213 = studentMapper213.selectStudentById(1);
System.out.println(student213);
}
@Test
public void deleteStudentById() {
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
studentMapper213.deleteStudentById(19);
sqlSession.commit();
}
@After
public void after() {
System.out.println("after方法执行结束");
}
3、mybatis的接口绑定方法
mybatis实现了接口绑定,使用会更加方便
接口绑定:mybatis中任意定义接口,然后把接口里面的方法和SQL语句绑定,就可以直接调用接口方法就可以
接口绑定方式:
3.1、通过注解绑定,直接在方法上面加上@Select @Update等注解,在注解里包含SQL绑定/3。3.2、通过XML里面SQL绑定,需要在xml配置文件里的namespace必须指定接口的全限定名
3.1、XML方法的方法
3.1.1、创建接口文件
public interface StudentMapper213 {
//通过id来查询某一个学生信息
public Student213 selectStudentById(Integer id);
}
3.2.2、创建mapper.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.tulun.mapper.StudentMapper213">
<select id="selectStudentById" parameterType="int" resultMap="studentMap">
select * from student where SID = #{id}
</select>
3.3.3、配置映射路径,在全局配置文件中导入
<!--映射文件引入-->
<mappers>
<mapper resource="mapper/studentmapper213.xml"/>
</mappers>
注意:编写mapper.java 接口和mapper.xml配置文件需要遵循一些开发规范,mybatis才能自动生成mapper接口的代理类对象
开发规范:
1、在mapper.xml配置文件中namespace等于接口的名称
2、使用的Statement的id属性值需要和接口中的方法名保持一致
3、接口文件方法参数和mapper.xml文件中parameterType指定类型一致
4、接口文件方法的返回值类型和mapper.xml中的resultType指定的类型一致
3.2、注解形式
注解方法SQL直接写在接口上
优点:比较简单,效率更高一些
缺点:当SQL发生改变都需要重新编译代码
mybatis的注解中,基本注解@Select 、@Insert()、@Update、@Delete
@Insert
@Options(useGeneratedKeys = true)
@Insert("insert into tulun (name) values(#{name})")
int insertTulun(Tulun213 tulun);
在方法上直接添加@Insert注解,将sql语句直接写在注解中
在插入操作需要添加自增主键等操作,需要使用@Options注解,该注解中包含useGeneratedKeys等操作
和xml配置对比
@Select
@Select("select * from tulun where id = #{id}")
Tulun213 selectTulunById(Integer id);
查询操作使用select注解直接在注解内给定SQL语句,如果数据库字段和映射pojo类属性名一致,则这样使用;也可以使用@results显性指定映射关系
Results注解类似xml方式中ResultMap便签
@Results注解对应XML中的ResultMap标签
@Result对应xml中的result标签或者是id标签(在result注解中后id=true)
4、输出映射
输出类型:resultType和resultMap
使用resultType,数据库的字段名和pojo类的属性名保持一致才能映射成功
如果字段名和属性名完全不一致,没有创建出自定义对象
如果字段名和属性名部分一致,会创建出自定义对象,对相同的字段和属性会完成映射
使用resultMap可以显性指定字段名和属性名的映射关系
不管输出是单个对象(selectOne)还是多个结果集(selectList),在mapper.xml中resultType或者resultMap的类型是一样的,不一样的是mapper.java 接口文件中返回值类型不一样
输出的是单个自定义的对象,方法返回单个对象的类型
输出的是多个对象的结果集,方法的返回是List<Object>
5、多个接口参数
public int updateNameById(Integer id, String name);
如果直接在接口中传递多个参数时,在sql执行过程中会抛出一下错误:
mybatis中多个参数时,mybatis有效的参数名只能是0,1,param1,param1等,当传递参数命名为其他的id或name时其不能识别,要么改成0,1等mybatis识别的参数,显然是无意义
如何解决,在传递的多个参数上添加注解@Param注解,mybatis就会自动封装成Map类型的数据,Param内的名称作为key,传递的数据作为值,在SQL执行过程中会通过入参来做解析
int updateNameById(@Param("id") Integer id,@Param("name") String name);
6、#{}和${}
#{}
select * from student where SID = #{id}
#{}使用类似于JDBC编程中preparedStatement的执行过程,在SQL中将参数通过‘?’占位符处理,将SQL和参数分别传递给SQL服务端
${}
select * from student where SID = ${id}
处理参数的获取使用ognl表达式,使用的参数必须提供getter方法
${}使用类似于JDBC中的Statement操作,在执行过程中将参数直接拼接到SQL上
#{}是不存在SQL注入问题
一般推荐使用#{}