发表于: 2019-10-23 19:05:35

1 904


今天完成的事情:

mybatis连接数据库


配置文件  mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
   <!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
   <!-- XML 配置文件包含对 MyBatis 系统的核心设置 -->
   <configuration>
   <!-- 指定 MyBatis 数据库配置文件 -->
   <properties resource ="db.properties" />
   <!-- 指定 MyBatis 所用日志的具体实现 -->
   <settings>
   <setting name="logImpl" value="LOG4J" />
   </settings>
   <environments default="development">
       <!-- 环境配置,即连接的数据库。 -->
       <environment id="development">
           <!-- 指定事务管理类型,type="JDBC"指直接简单使用了JDBC的提交和回滚设置 -->
           <transactionManager type="JDBC" />
           <!-- dataSource指数据源配置,POOLEDJDBC连接对象的数据源连接池的实现。 -->
           <dataSource type="POOLED">
               <property name="driver" value="${driver}" />
               <property name="url" value="${url}" />
               <property name="username" value="${username}" />
               <property name="password" value="${password}" />
           </dataSource>
       </environment>
   </environments>
<!-- mappers告诉了MyBatis去哪里找持久化类的映射类(注解形式) -->
<mappers>
   <mapper resource ="mappers/StudentMapper.xml" />
</mappers>
</configuration>


映射文件  Studentmapper.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:命名空间,随便写,一般保证命名空间唯一 -->
       <mapper namespace="mappers.StudentMapper">
       <!-- statement,内容:sql语句。id:唯一标识,随便写,在同一个命名空间下保持唯一
       resultTypesql语句查询结果集的封装类型,tb_user即为数据库中的表
       -->
       <select id="findStudentId" resultType="enity.Student">
       select * from bj where id = #{id}
</select>
       <select id="selectAllStudent" resultType="enity.Student">
       select * from bj
</select>
       <delete id="deleteStudent" parameterType="enity.Student">
       delete from bj where id = #{id}
</delete>
       <update id="updateStudent" parameterType="enity.Student">
       update bj set name = #{name} ,type=#{type} where id = #{id}
</update>
       <insert id="insertStudent"  keyProperty="id"   useGeneratedKeys="true"     parameterType="enity.Student">\
insert into bj (id,name, qq) values (#{id},#{name}, #{qq})
</insert>
       </mapper>

实体文件  Student


接口文件  Studentmapper

package enity;

public interface StudentMapper {

void  findStudentId();

           void  selectAllStudent();

           void  deleteStudent();

           void  updateStudent();

           void  insertStudent();

   }


增删查改

package enity;

import org.junit.Test;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentMybatis {


/////按id查找,  第一个有注释  其他的省略未写


@Test
   public void findStudentId() throws IOException {
//指定全局文件
       String resource = "mybatis-config.xml";

       //读取配置文件
       InputStream inputStream = Resources.getResourceAsStream(resource);

       //构建SqlSessionFactory
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

       //获取sqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession();

       //创建一个statement  值内容为StudentMapper.xml里的 namespace + 需要调用的id
       String statement = "mappers.StudentMapper.findStudentId";

       //statement可以用上方的"xx"内容替代  不用再创建statement
       Student student = sqlSession.selectOne(statement, 3);

       System.out.println(student);

       //关闭sqlsession
       sqlSession.close();
   }


//查找全部

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

       String statement = "mappers.StudentMapper.selectAllStudent";

       List<Student> students = sqlSession.selectList(statement);
       System.out.println(students);
       sqlSession.close();

   }


//删除

@Test
   public void deleteStudent() throws IOException {
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       SqlSession sqlSession = sqlSessionFactory.openSession();
       String statement = "mappers.StudentMapper.deleteStudent";

       Student student = sqlSession.delectOne(statement, 12);
       System.out.println(student);
       sqlSession.commit();
       sqlSession.close();

   }


//更改


@Test
   public void updateStudent() throws IOException {
       String resource = "mybatis-config.xml";
       InputStream inputStream = Resources.getResourceAsStream(resource);

       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

       SqlSession sqlSession = sqlSessionFactory.openSession();

       String statement = "mappers.StudentMapper.updateStudent";

       Student student = new Student();
       student.setName("王望");
       student.setType("后端");
       student.setId(22);

       int i = sqlSession.update(statement, student);

       if (i > 0) {
       System.out.println(true);
       } else {
        System.out.println(false);
        }
        sqlSession.commit();
        sqlSession.close();

       }



//插入



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

       String statement = "mappers.StudentMapper.insertStudent";

       Student student = new Student();

       student.setId(13);
       student.setName("王丽丽");
       student.setqq(15849996);
//        insert的()里,需要路径和student的具体的值
       sqlSession.insert(statement, student);

       System.out.println(student.getId());

       sqlSession.commit();
       sqlSession.close();
   }

   }


mybatis 流程:   

mybatis需要先指定全局配置文件,再读取配置文件,然后构建Sqlsession工厂,再利用SqlsessionFactory工厂获取sqlsession对象,使用它进行增删改查,最后调用sqlSession.commit提交事务,使数据入库,关闭sqlSession



前面   改 增   出错的原因

是因为我实体文件     set方法(里没有加     类型及参数   导致一直出错

 

出错的原因  是因为sqlSession     s小写而不是大写  


果然要细心啊  不然排错要排很久




List的基础,常用方法:
(1)      List<T>mList = new List<T>(  ) ;
T为列表中元素类型,现在以String类型作为例子
举例:List<String>mList = new List<String>(  );
(2)     List<T>testList = new List<T>(IEnumerable<T>collection);
以一个集合作为参数创建List
举例:String[ ] temArr = {"sss","地区","张三","里斯","王二","麻子"};
List<String> testList = new list<String>(temArr);


明天计划的事情:

把jdbc      jdbctemplate     mybatis   连接数据库再过一遍



继续进行任务


遇到的问题:


   ioc已经看了   spring的其他内容要看什么? 



收获:

对mybatis连接数据库   熟悉了些


不是熬夜选手,困,早点睡   明天早起学习






返回列表 返回列表
评论

    分享到