发表于: 2019-11-14 02:03:55

4 744


一、今天完成的事情

*完成MyBatis的代码运行:


       1.  创建maven项目,项目整体框架如下

       2.  添加依赖

       3.  编写Student.java类,描述实体,包含若干个实体的属性
package pojo;
public class Student {
    private int id;
    private String name;
    private String create_at;
    private String update_at;
    public Student(){
    }
    public Student(int id, String name, String create_at, String update_at) {
        this.id = id;
        this.name = name;
        this.create_at = create_at;
        this.update_at = update_at;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCreate_at() {
        return create_at;
    }
    public void setCreate_at(String create_at) {
        this.create_at = create_at;
    }
    public String getUpdate_at() {
        return update_at;
    }
    public void setUpdate_at(String update_at) {
        this.update_at = update_at;
    }
    @Override
    public String toString() {
        return "Student{" + "ID=" + id + ", Name='" + name + "'" +
                ", Created at=" + create_at + ", Updated at=" + update_at+"}";
    }
}
       4.  编写StudentMapper.java接口,体现实体在数据库的行为,对实体行为进行描述
package mapper;
import pojo.Student;
public interface StudentMapper {
    void insertStudent(Student student);
    Student queryById(int id);
    void updateStudent(int id);
}
       5.  编写Student.xml,这是实体行为在数据库操作上的具体实现,包含了sql代码和映射定义信息
<?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="mapper.StudentMapper">
    <insert id="insertStudent" parameterType="pojo.Student">
        INSERT INTO STUDENT(ID,NAME,CREATE_AT,UPDATE_AT) VALUES(#{id},#{name},#{create_at},#{update_at})
    </insert>
    <select id="queryById" parameterType="int" resultType="pojo.Student">
        SELECT * FROM STUDENT WHERE ID=#{ID}
    </select>
    <update id="updateStudent">
        UPDATE STUDENT SET NAME=#{name},CREATE_AT=#{create_at},UPDATE_AT=#{update_at} WHERE ID=#{id}
    </update>
</mapper>
       
        6.  编写mybatis-config.xml,包含数据库连接配置、Student类的mapper映射器
<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver”/> //第一遍时没加cj报错
                <property name="url" value="jdbc:mysql://localhost:3306/entryform"/>
                <property name="username" value="root"/>
                <property name="password" value="rootpass"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/Student.xml"/>
    </mappers>
</configuration>
           
        7.  测试
//import javax.annotation.Resources;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import mapper.StudentMapper;
import pojo.Student;
import org.apache.ibatis.io.Resources;
public class Test {
    @org.junit.Test
    public void testInsertStudent() throws IOException {
        //get mybatis-config.xml
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        try {
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = new Student();
            //student.setId(2);
            student.setName("rueben");
            student.setCreate_at("20191113");
            student.setUpdate_at("20191113");
            studentMapper.insertStudent(student);
            sqlSession.commit();
        } finally {
            sqlSession.close();
        }
    }
    
    @org.junit.Test
    public void testQueryById() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        try {
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = studentMapper.queryById(1);
            System.out.println(student);
        } finally {
            sqlSession.close();
        }
        
    }
}


二、遇到的问题


1.第一遍运行的时候getResourceAsStream报错,cannot resolve method,折腾了半天,最后发现Resource这个类不止一个定义,当引入包javax.annotation.Resources;时,Resources并不提供getResourceAsStream这个方法,只有当引入包org.apache.ibatis.io.Resources时,才有这个方法。

2.

  1. 这个虽然不知道原因但是跟着改就行了。
  2. 3.第一遍运行的时候querytest通过了,inserttest没有通过,报错太多懒得看,自己想了想,可能是因为已经在数据库里把id设为自增类,但是实体类student.java里给id写了setter和getter,会不会是这个问题,把setter删掉之后运行通过。
  3. 4.create_at和update_at这两天为了偷懒直接改成String类型然后手动赋值了,这个后面再改吧~
三、明天的计划

       1.  把create和update以及其他fields补上
       2.  总结jdbctemplate和mybatis的区别
       3.  玩云服务器 



返回列表 返回列表
评论

    分享到