MyBatis复习【简单配置CRUD】

时间:2023-12-16 08:24:32

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

MyBatis复习【简单配置CRUD】

用到的jar文件

MyBatis复习【简单配置CRUD】

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

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

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

 /**
* 添加新的学生
* @param stu
* @return 返回 该insert语句成功后的自增列
* @throws IOException
*/
public int SaveInfo(Student stu) throws IOException; /**
* 根据id删除学生信息
* @param stuno
* @return
* @throws IOException
*/
public int DeleteInfo(int stuno) throws IOException; /**
* 根据学生对象 模糊查询学生信息
*/
public List<Student> getAllInfoByStudent(Student stu) throws IOException; /**
* 根据学生姓名 模糊查询学生信息
*/
public List<Student> getAllInfoByName(String stuName) throws IOException;

书写doa对应的配置文件

 <?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="cn.zym.mybatis.dao">
<!-- <select id="addStudent" parameterType="cn.zym.mybatis.entity.Student">
insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
</select> -->
<insert id="addStudent" parameterType="Student">
insert into Student(stuname,stuage) values(#{stuName},#{stuAge})
<selectKey keyProperty="stuNo" resultType="_integer" order="AFTER">
<!-- oracle需要设置order为BEFORE :select seq_ssm.currval from dual(); -->
select @@identity
</selectKey>
</insert> <delete id="deleteInfo" parameterType="int">
delete from student where stuno=#{stuno}
</delete> <select id="getAllInfoByStudent" parameterType="Student" resultType="Student">
select * from student s where s.stuname like concat('%',#{stuName},'%')
</select> <select id="getAllInfoByName" resultType="Student">
select * from student s where s.stuname like '%${value}%'<!-- 此处若要使用${xxx}的写法,其值必须为"value" -->
</select> </mapper>

StudentDao.xml

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

 <?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>
<!-- 配置cn.zym.mybatis.entity包下的所有bean的别名为简单类名; -->
<typeAliases>
<package name="cn.zym.mybatis.entity"/>
</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://localhost:3306/zhangyiming" />
<property name="username" value="zym" />
<property name="password" value="admin" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/zym/mybatis/dao/StudentDao.xml" />
</mappers>
</configuration>

Mybatis-config.xml

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

StudentDaoImpl

 package cn.zym.mybatis.dao.impl;

 import java.io.IOException;
import java.io.Reader;
import java.util.List; 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 cn.zym.mybatis.dao.IStudentDao;
import cn.zym.mybatis.entity.Student;
import cn.zym.mybatis.utils.MybatisUtils; public class StudentDaoImpl implements IStudentDao { /*@Override
public int SaveInfo(Student stu) throws IOException {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
int insert = session.insert("addStudent",stu);
session.commit();
session.close();
return insert;
}*/ @Override
public int SaveInfo(Student stu) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
int insert = sqlSession.insert("addStudent",stu);
sqlSession.commit();
sqlSession.close();
return insert;
} @Override
public int DeleteInfo(int stuno) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
int delete = sqlSession.delete("deleteInfo",stuno);
sqlSession.commit();
return delete;
} @Override
public List<Student> getAllInfoByStudent(Student stu) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Student> list = sqlSession.selectList("getAllInfoByStudent",stu);
return list;
} @Override
public List<Student> getAllInfoByName(String stuName) throws IOException {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Student> list = sqlSession.selectList("getAllInfoByName",stuName);
return list;
} }

StudentDaoImpl

MybatisUtils

 package cn.zym.mybatis.utils;

 import java.io.IOException;
import java.io.Reader; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtils {
static Reader reader =null; static{
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
public static SqlSession getSqlSession(){
SqlSession session = factory.openSession();
return session; } }

MybatisUtils

测试code

 package cn.zym.mybatis.test;

 import java.io.IOException;
import java.util.List; import org.junit.Before;
import org.junit.Test; import cn.zym.mybatis.dao.IStudentDao;
import cn.zym.mybatis.dao.impl.StudentDaoImpl;
import cn.zym.mybatis.entity.Student; public class Mytest {
IStudentDao dao ;
@Before
public void initalData(){
dao= new StudentDaoImpl();
} /**
* 模糊 姓名 查询学生信息
*/
@Test
public void queryStudentByBean() throws IOException{
List<Student> list = dao.getAllInfoByStudent(new Student("zym2",));
for (Student student : list) {
System.out.println(student);
}
System.out.println("ok!");
} /**
* 模糊 姓名 查询学生信息
*/
@Test
public void queryStudentByString() throws IOException{
List<Student> list = dao.getAllInfoByName("");
for (Student student : list) {
System.out.println(student);
}
System.out.println("ok!");
} /**
* 删除学生信息
*/
@Test
public void deleteStudent() throws IOException{
dao.DeleteInfo();
System.out.println("ok!");
} /**
* 添加学生信息
* @throws IOException
*/
@Test
public void addStudent() throws IOException{
IStudentDao dao = new StudentDaoImpl();
Student stu = new Student("zym", );
System.out.println("保存前:"+stu);
dao.SaveInfo(stu);
System.out.println("save ok!");
System.out.println("保存后:"+stu);
}
}

test code