首页 > 代码库 > MyBatis复习【简单配置CRUD】

MyBatis复习【简单配置CRUD】

这里的案例集成了log4j的日志框架,项目架构:

 

技术分享

 用到的jar文件

技术分享

添加配置文件:mybatis-config.xml  和dao层配置文件StudentDao.xml

 

这里书写了个简单的案例仅为了说明问题

dao中有几个增删改查的抽象方法

 1 /**
 2      * 添加新的学生
 3      * @param stu
 4      * @return 返回 该insert语句成功后的自增列
 5      * @throws IOException
 6      */
 7     public int SaveInfo(Student stu) throws IOException;
 8     
 9     /**
10      * 根据id删除学生信息
11      * @param stuno
12      * @return
13      * @throws IOException
14      */
15     public int DeleteInfo(int stuno) throws IOException;
16     
17     /**
18      * 根据学生对象 模糊查询学生信息
19      */
20     public List<Student> getAllInfoByStudent(Student stu) throws IOException;
21     
22     /**
23      * 根据学生姓名 模糊查询学生信息
24      */
25     public List<Student> getAllInfoByName(String stuName) throws IOException;
26     

 

书写doa对应的配置文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="cn.zym.mybatis.dao">
 6     <!-- <select id="addStudent" parameterType="cn.zym.mybatis.entity.Student">
 7         insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
 8     </select> -->
 9     <insert id="addStudent" parameterType="Student">
10         insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
11         <selectKey keyProperty="stuNo" resultType="_integer" order="AFTER">
12         <!-- oracle需要设置order为BEFORE   :select seq_ssm.currval from dual(); --> 
13             select @@identity
14         </selectKey>
15     </insert>
16     
17     <delete id="deleteInfo" parameterType="int">
18         delete from student where stuno=#{stuno}
19     </delete>
20     
21     <select id="getAllInfoByStudent" parameterType="Student" resultType="Student">
22         select * from student s where s.stuname like concat(%,#{stuName},%)
23     </select>
24     
25     <select id="getAllInfoByName" resultType="Student">
26         select * from student s where s.stuname like %${value}%<!-- 此处若要使用${xxx}的写法,其值必须为"value" -->
27     </select>
28     
29     
30     
31     
32 </mapper>
StudentDao.xml

 

在dao中使用的别名在大配置文件中设置别名:

技术分享
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <!-- 配置cn.zym.mybatis.entity包下的所有bean的别名为简单类名; -->
 7     <typeAliases>
 8         <package name="cn.zym.mybatis.entity"/>
 9     </typeAliases>
10     
11     <environments default="development">
12         <environment id="development">
13             <transactionManager type="JDBC" />
14             <dataSource type="POOLED">
15                 <property name="driver" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />
16                 <property name="url" value=http://www.mamicode.com/"jdbc:mysql://localhost:3306/zhangyiming" />
17                 <property name="username" value=http://www.mamicode.com/"zym" />
18                 <property name="password" value=http://www.mamicode.com/"admin" />
19             </dataSource>
20         </environment>
21     </environments>
22     <mappers>
23         <mapper resource="cn/zym/mybatis/dao/StudentDao.xml" />
24     </mappers>
25 </configuration>
Mybatis-config.xml

 

这两个文件中的头文件都可以到附带的pdf文档中获取   Getting started目录节点下可找到;

 

 StudentDaoImpl

技术分享
 1 package cn.zym.mybatis.dao.impl;
 2 
 3 import java.io.IOException;
 4 import java.io.Reader;
 5 import java.util.List;
 6 
 7 import org.apache.ibatis.io.Resources;
 8 import org.apache.ibatis.session.SqlSession;
 9 import org.apache.ibatis.session.SqlSessionFactory;
10 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
11 
12 import cn.zym.mybatis.dao.IStudentDao;
13 import cn.zym.mybatis.entity.Student;
14 import cn.zym.mybatis.utils.MybatisUtils;
15 
16 public class StudentDaoImpl implements IStudentDao {
17 
18     /*@Override
19     public int SaveInfo(Student stu) throws IOException {
20         Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
21         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
22         SqlSession session = factory.openSession();
23         int insert = session.insert("addStudent",stu);
24         session.commit();
25         session.close();
26         return insert;
27     }*/
28     
29     @Override
30     public int SaveInfo(Student stu) throws IOException {
31         SqlSession sqlSession = MybatisUtils.getSqlSession();
32         int insert = sqlSession.insert("addStudent",stu);
33         sqlSession.commit();
34         sqlSession.close();
35         return insert;
36     }
37 
38     @Override
39     public int DeleteInfo(int stuno) throws IOException {
40         SqlSession sqlSession = MybatisUtils.getSqlSession();
41         int delete = sqlSession.delete("deleteInfo",stuno);
42         sqlSession.commit();
43         return delete;
44     }
45 
46     @Override
47     public List<Student> getAllInfoByStudent(Student stu) throws IOException {
48         SqlSession sqlSession = MybatisUtils.getSqlSession();
49         List<Student> list = sqlSession.selectList("getAllInfoByStudent",stu);
50         return list;
51     }
52 
53     @Override
54     public List<Student> getAllInfoByName(String stuName) throws IOException {
55         SqlSession sqlSession = MybatisUtils.getSqlSession();
56         List<Student> list = sqlSession.selectList("getAllInfoByName",stuName);
57         return list;
58     }
59 
60 }
StudentDaoImpl

 

MybatisUtils

技术分享
 1 package cn.zym.mybatis.utils;
 2 
 3 import java.io.IOException;
 4 import java.io.Reader;
 5 
 6 import org.apache.ibatis.io.Resources;
 7 import org.apache.ibatis.session.SqlSession;
 8 import org.apache.ibatis.session.SqlSessionFactory;
 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
10 
11 public class MybatisUtils {
12     static Reader reader =null;
13     
14     static{
15         try {
16             reader = Resources.getResourceAsReader("mybatis-config.xml");
17         } catch (IOException e) {
18             e.printStackTrace();
19         }
20     }
21     static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
22     public static SqlSession getSqlSession(){
23         SqlSession session = factory.openSession();
24         return session;
25         
26     }
27     
28     
29     
30 }
MybatisUtils

 

 测试code

技术分享
 1 package cn.zym.mybatis.test;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 import cn.zym.mybatis.dao.IStudentDao;
10 import cn.zym.mybatis.dao.impl.StudentDaoImpl;
11 import cn.zym.mybatis.entity.Student;
12 
13 public class Mytest {
14     IStudentDao dao ;
15     @Before
16     public void initalData(){
17         dao=  new StudentDaoImpl();
18     }
19     
20     /**
21      * 模糊  姓名  查询学生信息
22      */
23     @Test
24     public void queryStudentByBean() throws IOException{
25         List<Student> list = dao.getAllInfoByStudent(new Student("zym2",21));
26         for (Student student : list) {
27             System.out.println(student);
28         }
29         System.out.println("ok!");
30     }
31     
32     /**
33      * 模糊  姓名  查询学生信息
34      */
35     @Test
36     public void queryStudentByString() throws IOException{
37         List<Student> list = dao.getAllInfoByName("3");
38         for (Student student : list) {
39             System.out.println(student);
40         }
41         System.out.println("ok!");
42     }
43     
44     
45     /**
46      * 删除学生信息
47      */
48     @Test
49     public void deleteStudent() throws IOException{
50         dao.DeleteInfo(4);
51         System.out.println("ok!");
52     }
53     
54     /**
55      * 添加学生信息
56      * @throws IOException
57      */
58     @Test
59     public void addStudent() throws IOException{
60         IStudentDao dao =  new StudentDaoImpl();
61         Student stu = new Student("zym", 21);
62         System.out.println("保存前:"+stu);
63         dao.SaveInfo(stu);
64         System.out.println("save ok!");
65         System.out.println("保存后:"+stu);
66     }
67 }
test code

 

MyBatis复习【简单配置CRUD】