目录

- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="StudentMapper">
- <select id="selectAll" resultType="demo1.bean.Student">
- SELECT * FROM student
- select>
- <select id="selectById" resultType="demo1.bean.Student" parameterType="java.lang.Integer">
- SELECT * FROM student WHERE sid = #{sid}
- select>
- mapper>

- package demo1.bean;
-
- public class Student {
- private Integer sid;
- private String name;
- private Integer age;
-
- public Student() {
- }
-
- public Student(Integer sid, String name, Integer age) {
- this.sid = sid;
- this.name = name;
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + sid +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- public Integer getId() {
- return sid;
- }
-
- public void setId(Integer sid) {
- this.sid = sid;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
- }

- package demo1.dao;
-
- import demo1.bean.Student;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.*;
- import org.junit.Test;
-
- import java.io.InputStream;
- import java.util.List;
-
- public class StudentTest1 {
- @Test
- public void selectAll() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过SqlSession工厂对象获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //4.执行映射配置文件中的sql语句,并接收结果
- List
list = sqlSession.selectList("StudentMapper.selectAll"); - //5.处理结果
- for(Student stu : list){
- System.out.println(stu);
- }
- //6.释放资源
- sqlSession.close();
- is.close();
- }
- @Test
- public void selectById() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过SqlSession工厂对象获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //4.执行映射配置文件中的sql语句,并接收结果
- List
list = sqlSession.selectList("StudentMapper.selectById",3); - //5.处理结果
- for(Student stu : list){
- System.out.println(stu);
- }
- //6.释放资源
- sqlSession.close();
- is.close();
- }
- }

- <insert id="insert" parameterType="demo1.bean.Student">
- INSERT INTO student VALUES (#{sid},#{name},#{age})
- insert>

- @Test
- public void insert() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过SqlSession工厂对象获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //4.执行映射配置文件中的sql语句,并接收结果
- Student stu = new Student(4,"赵六",26);
- int result = sqlSession.insert("StudentMapper.insert",stu);
- //5.提交事务
- sqlSession.commit();
- //5.处理结果
- System.out.println(result);
- //6.释放资源
- sqlSession.close();
- is.close();
- }


- <update id="update" parameterType="demo1.bean.Student">
- UPDATE student SET name = #{name},age = #{age} WHERE sid = #{sid}
- update>

- @Test
- public void update() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过SqlSession工厂对象获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //4.执行映射配置文件中的sql语句,并接收结果
- Student stu = new Student(4,"倪炜豪",99);
- int result = sqlSession.insert("StudentMapper.update",stu);
- //5.提交事务
- sqlSession.commit();
- //5.处理结果
- System.out.println(result);
- //6.释放资源
- sqlSession.close();
- is.close();
- }


- <delete id="delete" parameterType="java.lang.Integer">
- DELETE FROM student WHERE sid = ${sid}
- delete>

- @Test
- public void delete() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过SqlSession工厂对象获取SqlSession对象
- SqlSession sqlSession = sqlSessionFactory.openSession();
- //4.执行映射配置文件中的sql语句,并接收结果
- int result = sqlSession.insert("StudentMapper.delete",4);
- //5.提交事务
- sqlSession.commit();
- //5.处理结果
- System.out.println(result);
- //6.释放资源
- sqlSession.close();
- is.close();
- }
