Mybatis使用注解完成CRUD操作
使用注解比xml文件方便简单许多
这里完成最简单的CRUD操作
如果读者想使用的话,只需要修改数据库名就可以直接使用。
详细的解释大家可以看上一篇博文。
首先看实体类
package hym.bean;
public class student {
private int id;
private String name;
private int age;
private float score;
private int tid; //这是teacherID
public student() {
}
public student(int id, String name, int age, float score, int tid) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
this.tid = tid;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
@Override
public String toString() {
return "student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", score=" + score +
", tid=" + tid +
'}';
}
}
再来看看mapper
就是在mapper接口中使用注解来完成CRUD
package hym.mapper;
import hym.bean.student;
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 java.util.List;
public interface StudentMapper {
@Select("select * from student")
List<student> SeletALL();
@Delete("delete from student where id = #{id}")
int DeleteStudent(int id);
@Insert("insert into student values(#{id},#{name},#{age},#{score},#{tid})")
int InsertStudent(student student);
@Update("update student set score = #{score} where id = #{id}")
int UpdateStudent(student student);
}
最后看看主要的实现方法
package hym;
import hym.bean.student;
import hym.mapper.StudentMapper;
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 main {
public static void main(String[] args) {
InputStream inputStream = null;
SqlSessionFactory sqlSessionFactory = null;
try {
inputStream = Resources.getResourceAsStream("mybatis-cfg.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
SqlSession sqlSession = sqlSessionFactory.openSession();
//使用xml文件方法
/*List<Teacher> objects = sqlSession.selectList("com.hym.SelectTeacherAll");
for(Teacher s : objects){
System.out.println(s.toString());
}*/
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//查询全部
/*List<student> students = studentMapper.SeletALL();
for (student s : students){
System.out.println(s.toString());
}*/
//增加学生
/*student student = new student(8,"小勇",23,67,1);
int i = studentMapper.InsertStudent(student);
System.out.println(i);*/
//删除学生
/*int i = studentMapper.DeleteStudent(8);
System.out.println(i);*/
//修改学生信息,此处修改学生分数
student student = new student();
student.setId(1);
student.setScore(100);
int i = studentMapper.UpdateStudent(student);
System.out.println(i);
sqlSession.commit();
sqlSession.close();
}
}
配置文件
<?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>
<!--配置数据库 别名等-->
<!--配置别名-->
<!--<typeAliases>
<!–<typeAlias type="com.hym.bean.Student" alias="stu"/>–>
<package name="hym.bean"/>
</typeAliases>-->
<!--environments 中可放置多个数据库连接方式, default后接的值 表示选择的是哪一个数据库 -->
<environments default ="mysql">
<!-- 声明当前是用的数据库环境 -->
<environment id = "mysql">
<!-- 是用原生JDBC事务 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 使用数据库连接词 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/edu"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="hym.mapper"/>
</mappers>
</configuration>