发表于: 2020-06-10 21:48:55
1 1725
今天完成的事:
1.用注解的方式完成mybatis的增删改查
2.学会了resultMap在单表查询中的简单使用
3.解决了昨天解决不了的这个类型的报错.
解决办法就是:和
要相互对应,才能找到要执行的方法.
今天的代码和测试
直接在接口的方法上添加注解,然后编写sql语句
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface StudentDao {
@Insert(" insert into student(name,enter_Time,qq,school,student_Number,type,log_Link,slogan,brother,create_At,update_At)" +
" values (#{name},#{enter_Time},#{qq},#{school},#{student_Number},#{type},#{log_Link},#{brother},#{slogan},#{create_At},#{update_At})")
void add(Student student);
@Delete(" delete from student where id=#{id}")
void deleteById(int id);
@Update("update student set name=#{name},update_At=#{update_At} where id=#{id};")
void updateById(Student student);
@Select("select *from student where id=#{id}")
Student findById(int id);
@Select(" select *FROM student")
List<Student> findAll();
}
测试类
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
//注解方法测试类
public class TestAnnotation {
private SqlSession sqlSession;
private InputStream in;
private StudentDao studentDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
sqlSession = factory.openSession();
studentDao = sqlSession.getMapper(StudentDao.class);
}
@After
public void close() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
@Test
public void testAdd() {
Student student = new Student();
student.setName("韦延伦");
student.setSchool("贵航");
student.setEnter_Time("20200605");
student.setStudent_Number("6601");
student.setType("JAVA工程师");
student.setQq("939070310");
student.setSlogan("javaer");
student.setLog_Link("http://www.jnshu.com/school/32609/daily");
student.setBrother("师兄");
for (int x = 0; x < 10; x++) {
long startTime = System.currentTimeMillis();
student.setCreate_At(startTime);
studentDao.add(student);
}
}
@Test
public void testDelete() {
studentDao.deleteById(2);
}
@Test
public void testUpdateById() {
Student s = new Student();
s.setName("韦延呈");
s.setId(1);
long updateTime = System.currentTimeMillis();
s.setUpdate_At(updateTime);
studentDao.updateById(s);
}
@Test
public void testFindById() {
Student s = studentDao.findById(1);
System.out.println(s.toString());
}
@Test
public void testFindAll() {
List<Student> list = studentDao.findAll();
for (Student student : list) {
System.out.println(student.toString());
}
}
}
测试结果
插入结果: 删除id为2的学生结果
更新id为2的学生为新名字
根据id查找结果
tip: 1.resultType的使用须知:查询数据库返回的结果集字段必须要跟实例类pojo的字段完全一样,也就是数据库表的列名一定要等于实例类的字段名.
2.通过学习和测试resultMap,resultMap可以通过设置数据库表的列和实例类的字段名一一对应来实现返回的结果集,下面是具体实现方法
(仅查询单表,这个结果集最大的作用是进行多表查询映射,由于还没试多表查询sql语句,这次就仅仅试它的基本特性)
2.1配置
<?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.jnshu.R_dao">
<!-- column:数据库的列名 property:要映射到实体类的字段名-->
<resultMap id="rMap" type="com.jnshu.ResultMapper_poji">
<!--要映射到实例类的主键ID-->
<id column="id" property="test_id"/>
<!--普通属性-->
<result column="name" property="test_name"/>
<result column="school" property="test_school"/>
</resultMap>
<select id="selectById" parameterType="java.lang.Integer" resultMap="rMap">
select *from student where id=#{value}
</select>
</mapper>
配好resultMap的id,type是实体类的全命名
<id>标签数据库和实体类字段名id之间的对应。
<result>标签是普通属性对应,column是数据库表的列名,property是实体类字段名
2.2实体类:
package com.jnshu;
public class ResultMapper_poji {
public int test_id;
public String test_name;
public String test_school;
2.3测试接口:
public interface R_dao {
ResultMapper_poji selectById(int id);
}
2.4测试类和测试结果
本次查询一共返回12列的数据,但是使用resultMap通过配置可以做到只获取4列映射到实体类.
明天计划的事情
1.练习sql语句的一对一,一对多的多表查询,学习resultMap的多表查询用法.
2.学习spring的简单使用.
遇到的问题:
1.使用mybatis的注解时Mapper.xml文件找不到接口
解决办法:使用注解时,要这样在全局配置文件中这样配置Mapper,才搜索到包里面接口的方法.
收获:通过今天的学习,学到resultMap的基本原理,mybatis的注解使用方法.
评论