1、前期准备
项目骨架图如下所示
1、配置conf.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值 和 environment 的 id 来指定mybatis运行时的数据库环境 -->
<environments default="development">
<!-- 开发环境(本机)、environment可以有多套 -->
<environment id="development">
<!-- 事务提交方式
JDBC:利用JDBC处理事务(commit rollback close)
MANAGED:将事务交给其他组件托管(Spring、jobss),默认情况下会关闭连接。
-->
<transactionManager type="JDBC"/>
<!-- 数据源类型
POOLED:使用数据库连接池
UNPOOLED:传统的JDBC模式(消耗性能、不推荐)
JNDI:从tomcat中获取内置数据库连接池
-->
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xiao?serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射文件 -->
<mapper resource="com/feng/entity/studentMapper.xml"/>
</mappers>
</configuration>
2、配置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">
<!--namespace:该mapper.xml映射文件的唯一标识-->
<mapper namespace="com.feng.entity.studentMapper">
<!-- 通过 namespace.id来定位该sql -->
<!-- 各种标签的id;
parameterType:输入参数的类型;
resultType:查询的返回结果的类型(具体对象映射到路径)
-->
<select id="queryStudentByStuNo" parameterType="int" resultType="com.feng.entity.Student">
select * from student where stuno = #{stuNo}
</select>
<select id="queryAllStudents" resultType="com.feng.entity.Student">
select * from student
</select>
<insert id="addStudent" parameterType="com.feng.entity.Student">
insert into student(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName})
</insert>
<update id="updateStudentByStuNo" parameterType="com.feng.entity.Student">
update student set stuname=#{stuName},stuage=#{stuAge},graname=#{graName} where stuno=#{stuNo}
</update>
<delete id="deleteStudentByStuNo" parameterType="int">
delete from student where stuno = #{stuNo}
</delete>
</mapper>
3、写好学生类和创建对应的表
package com.feng.entity;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
private String graName;
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getGraName() {
return graName;
}
public void setGraName(String graName) {
this.graName = graName;
}
public Student(int stuNo, String stuName, int stuAge, String graName) {
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.graName = graName;
}
public Student() {
}
@Override
public String toString() {
return '{'+"stuNo=" + stuNo +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
", graName='" + graName + '\'' +
'}';
}
}
2、编写测试类进行测试
package com.feng.entity;
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.Reader;
import java.util.List;
public class Test {
// 1、查询单个学生
public static void queryStudentByStuNo() throws IOException {
// conf.xml ----> reader
Reader reader = Resources.getResourceAsReader("com/feng/conf.xml");
// reader ---> SqlSession
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
// arg1: namespace + id 确定 sql 、 arg2: 输入参数
String statement = "com.feng.entity.studentMapper.queryStudentByStuNo";
Student student = session.selectOne(statement, 1);
System.out.println(student);
session.close();
}
// 2、查询全部学生
public static void queryAllStudents() throws IOException {
Reader reader = Resources.getResourceAsReader("com/feng/conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
// arg1: namespace + id 确定 sql 、 arg2: 输入参数
String statement = "com.feng.entity.studentMapper.queryAllStudents";
List<Student> students = session.selectList(statement);
System.out.println(students);
session.close();
}
// 3、增加学生
public static void addStudent() throws IOException {
Reader reader = Resources.getResourceAsReader("com/feng/conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "com.feng.entity.studentMapper.addStudent";
Student student = new Student(3, "柴犬", 25, "六年级");
// statement:指定执行的sql、student:sql中需要的参数
int count = session.insert(statement, student);
session.commit(); // 提交事务
System.out.println("增加了"+count+"个学生");
session.close();
}
// 4、删除学生
public static void updateStudentByStuNo() throws IOException {
Reader reader = Resources.getResourceAsReader("com/feng/conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "com.feng.entity.studentMapper.updateStudentByStuNo";
Student student = new Student();
// 修改那个人, where stuNo = 2
student.setStuNo(2);
// 修改成啥样子
student.setStuName("三哈");
student.setStuAge(25);
student.setGraName("六年级");
int count = session.update(statement, student);
session.commit(); // 提交事务
System.out.println("修改"+count+"个学生");
session.close();
}
// 4、增加学生
public static void deleteStudentByStuNo() throws IOException {
Reader reader = Resources.getResourceAsReader("com/feng/conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
String statement = "com.feng.entity.studentMapper.deleteStudentByStuNo";
int count = session.delete(statement, 3);
session.commit(); // 提交事务
System.out.println("删除了"+count+"个学生");
session.close();
}
public static void main(String[] args) throws IOException {
queryAllStudents();
addStudent();
// deleteStudentByStuNo();
updateStudentByStuNo();
queryAllStudents();
}
}