Mybtis(三) 通过注解实现CRUD操作
mybatis 通过注解实现CRUD 操作 只需要一个配置文件Configuration.xml 名字自己随意起 只是在读取的时候名字一致就可以,因为是通过注解所以不需要映射文件
1.0 环境准备 同上一篇
2.0 目录结构
3.0 编码
3.1 配置文件内容 Configuation.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"/>
<dataSource type="POOLED">
<!-- //设置驱动 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- //链接 -->
<property name="url" value="jdbc:mysql://localhost:3306/xyd" />
<!-- //用户名 -->
<property name="username" value="root"/>
<!-- //密码 -->
<property name="password" value="xuyandong"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- //映射文件地址 -->
<mapper class="com.ccut.DAO.CRUD"/>
</mappers>
</configuration>
这里注意 mapper下的地址为对应类的完全限定名不是映射文件了
3.2创建student表 如下:
3.3 创建数据库模型 Student.java
package com.ccut.domain;
public class Student {
private int id;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
3.4 创建接口类 CRUD.java 这里也是用注解的地方
package com.ccut.DAO;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.ccut.domain.Student;
public interface CRUD {
@Select("select * from student where id = #{id}")
public Student selectDATA(int id);
@Insert("insert into student(id,name,sex) values(#{id},#{name},#{sex})")
public void insertDATA(Student student);
@Update("update student set name = #{name}, sex = #{sex} where id = #{id}")
public void updataDATA(Student student);
@Delete("delete from student where id = #{id}")
public void deleteDATA(int id);
@Select("select * from student")
public List<Student> selectAll();
}
3.5 构建Mybatis工具类 Util.java 用来读取配置文件同时构建sessionfactory 返回sqlsession
package com.ccut.MybatisUtil;
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 Util {
private static Reader reader;
private static SqlSessionFactory factory;
private static String cfg = "Configuration.xml";
private static void setSqlSessionFactory(){
try {
//读取配置文件
reader = Resources.getResourceAsReader(cfg);
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("找不到文件!");
e.printStackTrace();
}
}
public static SqlSession getSession(boolean yes){
setSqlSessionFactory();
return factory.openSession(yes);
}
}
这里注意 factory.openSession(yes); 中的 yes是布尔类型的 true 代表是自动提交,sql进行增删改的时候必须提交否则数据库不更新数据
3.6 由于调用接口中增删改查的时候还需要用到 session.getMapper(Student.calss);并且关闭会话,所以我们在业务层将上述操作封装,在有需要的时候直接调用即可 封装实现类CRUDimp.java
package com.ccut.service;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.ccut.DAO.CRUD;
import com.ccut.MybatisUtil.Util;
import com.ccut.domain.Student;
public class CRUDimp {
public void insert(Student student){
SqlSession session = Util.getSession(true);
CRUD crud = session.getMapper(CRUD.class);
crud.insertDATA(student);
session.close();
}
public void delete(int id){
SqlSession session = Util.getSession(true);
CRUD crud = session.getMapper(CRUD.class);
crud.deleteDATA(id);
session.close();
}
public void update(Student student){
SqlSession session = Util.getSession(true);
CRUD crud = session.getMapper(CRUD.class);
crud.updataDATA(student);
session.close();
}
public void selectOne(int id){
SqlSession session = Util.getSession(true);
CRUD crud = session.getMapper(CRUD.class);
Student student = crud.selectDATA(id);
session.close();
System.out.println("id = "+student.getId()+" name = "+student.getName()+" sex = "+student.getSex());
}
public void selectAll(){
SqlSession session = Util.getSession(true);
CRUD crud = session.getMapper(CRUD.class);
List<Student> list = crud.selectAll();
session.close();
for(Student student:list){
System.out.println("id = "+student.getId()+" name = "+student.getName()+" sex = "+student.getSex());
}
}
}
注意这里并没有实现接口CRUD 只是对其进行了二次封装 方便直接调用
3.7 编写测试类 Test.java
package test;
import com.ccut.domain.Student;
import com.ccut.service.CRUDimp;
public class Test {
public static void main(String[] args) {
CRUDimp cruDimp = new CRUDimp();
Student student = new Student();
// student.setId(1);
// student.setName("徐艳当");
// student.setSex("女");
student.setId(2);
student.setName("徐艳东");
student.setSex("女");
// cruDimp.insert(student);
// cruDimp.update(student);
cruDimp.selectOne(1);
System.out.println("----------------------------------");
cruDimp.selectAll();
cruDimp.delete(1);
System.out.println("删除后");
cruDimp.selectAll();
}
}
经测试 测试均通过 结果如下:
插入:
接着插入:
修改:
单个查询,全部查询 和 删除后全部查询:
至此OK 其他内容以后再说!
官方中文文档链接点击这里