发表于: 2017-10-13 23:22:48

2 718


今天完成的事

1,mybatis链接了一下数据库

新建一个Mybatis配置文件:

<?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格式对应文件,从而使得主配置文件更加通用。这样对部署非常有用  -->
   <!-- <properties resource="mysql.properties" /> -->
   <!-- 设置别名,一定要放在properties下面  -->
   <typeAliases>
       <typeAlias alias="Student3" type="DAO.Student3"  />
   </typeAliases>
   <!-- 配置数据源相关的信息  -->
   <environments default="development">
       <environment id="development">
           <transactionManager type="JDBC" />
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://127.0.0.1:3306/mysql"/>
               <property name="username" value="root"/>
               <property name="password" value="123"/>
               <!--<property name="driver" value="${driver}" />
                   <property name="url" value="${url}" />
                   <property name="username" value="${username}" />
                   <property name="password" value="${password}" /> -->
           </dataSource>
       </environment>
   </environments>
   <!-- 列出映射文件 -->
   <mappers>
       <mapper resource="Student3.xml" />
       <mapper resource="mapper/Student3Mapper.xml" />
       <!--<mapper class="mapper.Student3Mapper"/>-->
   </mappers>
</configuration>

新建表对应的类:

package DAO;

public class Student3 {


   private int id;
   private String user_name;
   private int user_qq;
   private String user_school;
   private String will;
   private int creat_at;
   private int update_at;


   public int getId() {
       return id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getUser_name() {
       return this.user_name;
   }

   public void setUser_name(String user_name) {
       this.user_name = user_name;
   }

   public int getUser_qq() {
       return user_qq;
   }

   public void setUser_qq(int user_qq) {
       this.user_qq = user_qq;
   }

   public String getUser_school() {
       return user_school;
   }

   public void setUser_school(String user_school) {
       this.user_school = user_school;
   }

   public String getWill() {
       return will;
   }

   public void setWill(String will) {
       this.will = will;
   }


   public int getUpdate_at() {
       return update_at;
   }

   public void setUpdate_at(int update_at) {
       this.update_at = update_at;
   }

   public int getCreat_at() {
       return this.creat_at;
   }

   public void setCreat_at(int creat_at) {
       this.creat_at = creat_at;
   }

   @Override
   public String toString() {
       return "Student3{" +
               "id=" id +
               ", user_name='" user_name '\'+
               ", user_qq=" user_qq +
               ", user_school='" user_school '\'+
               ", will='" will '\'+
               ", creat_at=" creat_at +
               ", update_at=" update_at +
               '}';
   }
}

配置数据映射

<?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="test">
   <!--查找语句
   #{}占位符,如果传入的是基本类型,那么{}变量名称可以随意写
  -->

   <select id="findStu" parameterType="int" resultType="Student3">
         select from student3 where id=#{id}
   </select>

   <!---->
   <insert id="insertStu" parameterType="Student3">
       <!--执行 SELECT LAST_INSERT_ID(),返回自增的主键。
       kepProperty:将返回的主键放入到传入参数的ID中保存。
       order:当前函数相对于insert语句执行顺序。在insert前执行是BEFORE,在insert执行是AFTER。
       -->
       <selectKey keyProperty="id" order="AFTER" resultType="int">
           SELECT last_insert_id()
       </selectKey>
       insert into student3 (user_name,user_qq,user_school,will,creat_at,update_at) VALUES
       (#{user_name},#{user_qq},#{user_school},#{will},#{creat_at},#{update_at})
   </insert>

   <update id="updateStu" parameterType="Student3">
       update student3 set user_name=#{user_name}, user_qq=#{user_qq}, user_school=#{user_school}, will=#{will} ,creat_at=#{creat_at} ,update_at=#{update_at} where id=#{id}
   </update>

   <delete id="deleteStu" parameterType="int">
       DELETE  from student3 where id=#{id}
   </delete>

</mapper>

测试类

package DAO;

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 java.io.IOException;
import java.io.InputStream;

public class TestStu {
   @Test
   public void findStu() throws IOException {
       String resource = "mybatis-config.xml";
       //通过流将核心配置文件读取进来
       InputStream inputStream = Resources.getResourceAsStream(resource);
       //通过核心文件控制流来创建会话工厂
       SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
       //通过工厂创建会话
       SqlSession sqlSession = factory.openSession();
       //第一个参数:所调用的sql语句=namespace+sql的id。
       Student3 student3 = sqlSession.selectOne("test.findStu"21);
       System.out.println(student3);


   }

   @Test
   public void insert() throws Exception {
       String resources = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resources);
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession = sqlSessionFactory.openSession();
       Student3 student3 = new Student3();
       student3.setUser_name("小达");
       student3.setUser_qq(12312312);
       student3.setUser_school("科大");
       student3.setWill("will");
       student3.setCreat_at(123);
       student3.setUpdate_at(978);
       //增加之前的ID
       System.out.println(student3.getId());
       sqlSession.insert("test.insertStu"student3);
       //提交事务
       sqlSession.commit();
       //增加之后的ID
       System.out.println(student3.getId());

   }

   @Test
   public void update() throws IOException {
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession = sqlSessionFactory.openSession();
       Student3 student3 = new Student3();
       student3.setId(29   );
       student3.setUser_name("小王八蛋");
       student3.setUser_qq(111111);
       student3.setUser_school("破科大");
       student3.setWill("Po");
       student3.setCreat_at(123);
       student3.setUpdate_at(789);

       sqlSession.update("test.updateStu"student3);
       sqlSession.commit();


   }

   @Test
   public void delete() throws IOException {
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession = sqlSessionFactory.openSession();
       sqlSession.delete("test.deleteStu"26);
       sqlSession.commit();

   }
}

编写DAO,Mapper动态代理方式(在收获里解释为什么用Mapper)

编写映射文件

<?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">
<!--
1,映射文件中namespace要等于接口的全路径名称
2,映射文件中sql语句id要等于接口的方法名称
3,映射文件中传入参数类型要等于接口传入方法的类型
4,映射文件中返回结果集类型要等于返回值类型
-->
<mapper namespace="mapper.Student3Mapper">
   <select id="findStu" parameterType="int" resultType="Student3">
       select * from student3 where id=#{id}
   </select>

   <!---->
   <insert id="insertStu" parameterType="Student3">
       <!--执行 SELECT LAST_INSERT_ID(),返回自增的主键。
       kepProperty:将返回的主键放入到传入参数的ID中保存。
       order:当前函数相对于insert语句执行顺序。在insert前执行是BEFORE,在insert执行是AFTER。
       -->
       <selectKey keyProperty="id" order="AFTER" resultType="int">
           SELECT last_insert_id()
       </selectKey>
       insert into student3 (user_name,user_qq,user_school,will,creat_at,update_at) VALUES
       (#{user_name},#{user_qq},#{user_school},#{will},#{creat_at},#{update_at})
   </insert>

   <update id="updateStu" parameterType="Student3">
       update student3 set user_name=#{user_name}, user_qq=#{user_qq}, user_school=#{user_school}, will=#{will} ,creat_at=#{creat_at} ,update_at=#{update_at} where id=#{id}
   </update>

   <delete id="deleteStu" parameterType="int">
       DELETE  from student3 where id=#{id}
   </delete>

</mapper>

编写接口(时间关系只做了查找和增加方法,其他类似。)

package mapper;

import DAO.Student3;

public interface Student3Mapper {
    Student3 findStu(int id)throws Exception;
    public void insertStu(Student3 student3)throws Exception;

}

编写测试类。

package mapper;

import DAO.Student3;
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.Before;
import org.junit.Test;

import java.io.InputStream;

public class TestStu {
   private SqlSessionFactory sqlSessionFactory;
   @Before
   public void setUp() throws Exception {
       //mybatis配置文件
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       //使用SqlSessionFactoryBuilder创建sessionFactory
       sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);
   }

   @Test
   public void testFindStu() throws Exception {
       SqlSession sqlSession=sqlSessionFactory.openSession();
       Student3Mapper student3Mapper=sqlSession.getMapper(Student3Mapper.class);
       Student3 student3=student3Mapper.findStu(28);
       System.out.println(student3);
       sqlSession.close();

   }
   @Test
   public void insertStu() throws Exception {
       SqlSession sqlSession=sqlSessionFactory.openSession();
       Student3Mapper student3Mapper=sqlSession.getMapper(Student3Mapper.class);
       Student3 student3=new Student3();
       student3.setUser_name("123");
       student3.setUser_qq(2113);
       student3.setUser_school("123");
       student3.setWill("7777");
       student3.setCreat_at(2);
       student3.setUpdate_at(2);
       student3Mapper.insertStu(student3);
       System.out.println(student3.getId());
       sqlSession.commit();
       sqlSession.close();
   }
}

收获

为什么用DAO层的实现用mapper动态注入而不使用Impl。

答,

  1.Impl的dao接口中存在大量模版方法,存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的数据库操作方法

  2.调用sqlSession方法时将statement的id硬编码了

  3.调用sqlSession传入的变量,由于sqlSession方法使用泛型,即使变量类型传入错误,在编译阶段也不报错,不利于程序开发。

【selectOneselectList的区别】


动态代理对象调用sqlSession.selectOne()和sqlSession.selectList()是根据mapper接口方法的返回值决定,如果返回list则调用selectList方法,如果返回单个对象则调用selectOne方法。


遇到的问题

【mybatis插入的时候一点小问题】

MAPPER动态注入的时候报错,一个小BUG怎么都干不掉。


后来发现IDEA的maven结构里,要想动态注入,类的结构和xml文件的路径必须是一样的。Eclipse里面可以直接放到一起,但是IDEA,必须这样再建一个文件夹放入进去,如图。

bug解除。

明天要做的事。

1,把mybatis还有一些知识点巩固下,例如db.properties文件的作用

2,开始学习Spring

3,学习Junit



返回列表 返回列表
评论

    分享到