发表于: 2020-06-09 23:39:24

1 1760


今天完成的事情

1.mybatis的增删改查

下面是实现步骤和测试结果

项目目录

1.在pom.xml导入mybatis依赖和log4j

2.配置mybatis-config.xml

<?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"/>
           <!-- 数据库连接相关配置 ,这里动态获取config.properties文件中的内容-->
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver" />
               <property name="url" value="jdbc:mysql://localhost:3306/apply?characterEncoding=utf8"/>
               <property name="username" value="root" />
               <property name="password" value="123456" />
           </dataSource>
       </environment>
   </environments>
   <!-- mapping文件路径配置 -->
   <mappers>
       <mapper resource="Mapper/StudentMapper.xml"/>
   </mappers>

</configuration>

ps:Mapper resource的路径一定要写对!

2.接口与接口实现类

package com.jnshu;

import java.util.List;

public interface StudentDao {
//增
   public void add(Student student);
   //根据ID删
   public void deleteById(int id);
   //根据ID更新用户
   public void updateById(Student student);
   //根据ID查询用户
   public Student findById(int id);
   //查询全部用户
   public List<Student> findAll();

}

package com.jnshu;

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 StudentDaoImpl implements StudentDao {
InputStream is;

   {
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
       } catch (IOException e) {
e.printStackTrace();
       }
}


SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

   SqlSessionFactory sqlSessionFactory = builder.build(is);
   SqlSession sqlSession=sqlSessionFactory.openSession(false);


   public void add(Student student) {
sqlSession.insert("insert",student);
       sqlSession.commit();

   }

public void deleteById(int id) {
sqlSession.delete("deleteById",id);
       sqlSession.commit();

   }

public void updateById(Student student) {
sqlSession.update("updateById",student);
       sqlSession.commit();

   }

public Student findById(int id) {
Student student=sqlSession.selectOne("findById");
       sqlSession.commit();

       return student;
   }

public List<Student> findAll() {
List<Student> list=sqlSession.selectList("selectAll");
       sqlSession.commit();
       return list;
   }
}

ps:这里因为设计接口实现类的方式出现偏差导致sqlSession.close()这个方法只能用一次,因为后面的测试类需要测试,所以就不关数据库连接池了

3.在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="com.jnshu">

   <select id="selectById" parameterType="java.lang.Integer" resultType="com.jnshu.Student">
     select from student where id=#{id}
</select>
   <select id="selectAll" resultType="com.jnshu.Student">
       select *FROM student
</select>
   <insert id="insert"  parameterType="com.jnshu.Student" >
       insert into student(name,enter_Time,qq,school)
values (#{name},#{enter_Time},#{qq},#{school})
</insert>
   <!--按id删除数据!-->
   <delete id="delete" parameterType="java.lang.Integer">
       delete *from Student where id=#{id}
</delete>

   <!-- 按id更新数据!-->
   <update id="update" parameterType="com.jnshu.Student">
      update student name=#{name } where id=#{id};
</update>
</mapper>

4.全部查找方法测试


遇到的问题:

1.这个文件是今天操心最多的,遇到的问题有 parameterType的Student路径名没写全,导致一直报错找不到 Student这个类。


这两个方法还在报错,暂时解决不了明天再解决

报错如下,都是一样的报错

明天计划的事情

1.学习mybatis的动态sql语句,resultMap这玩意,还有用注解的方式使用mybatis进行增删改查,继续理解这个框架的运行原理,争取少犯低级错误。

2.学习spring的DI注入


收获:

mybatis的基本使用流程。



返回列表 返回列表
评论

    分享到