一 CRUD基础方式
mybatis约定:
1 输入参数parameterType和输出参数resultType,在形式上都只能有一个
2 如果输入参数是简单类型(8个基本类型+String)是可以使用任何占位符,#{xxx} ;如果是对象类型,则必须是对象属性名。
3输出参数:如果返回值类型是一个对象(如Student),则无论返回一个还是多个,resultType都要写成com.liusong.entity.Student,即:resultType="com.liusong.entity.Student"
4 如果使用的事物的方式为jdbc,则需要手动提交session.commit()
5 所有的标签<select> <update>等,都必须有sql语句,但是sql参数可选
sql有参数:session.insert(statement,参数值)
sql无参数:session.insert(statement)
PersonMapper.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"> <!-- 指定映射文件路径 --> <mapper namespace="com.liusong.entity.PersonMapper"> <!-- parameterType输入参数的类型 mybatis约定在形式上输入输出都只能有一个 --> <select id="queryPersonById" parameterType = "int" resultType="com.liusong.entity.Person"> select * from Person where id= #{id} </select> <insert id="addPerson" parameterType="com.liusong.entity.Person"> insert into Person(id,name,age) values(#{id},#{name},#{age}) </insert> <update id="updatePersonById" parameterType="com.liusong.entity.Person"> update person set name = #{name} , age = #{age} where id = #{id} </update> <delete id="deletePersonById" parameterType="int"> delete from Person where id = #{id} </delete> <select id="queryAllPerson" resultType="com.liusong.entity.Person"> select * from person </select> </mapper>
TestDemo代码
package com.liusong.entity; 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; public class TestDemo { //查询单个学生 public static void queryPersonById() throws IOException { //将config.xml变为流 Reader reader = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.PersonMapper.queryPersonById"; Person person = session.selectOne(stament, 1); System.out.println("根据编号查询到的学生为:"+person); session.close(); } //查询所有学生 public static void queryAllPerson() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.PersonMapper.queryAllPerson"; List<Person> persons = session.selectList(stament); System.out.println("查询到所有学生信息为:"+persons); session.close(); } //增加学生 public static void addPerson() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.PersonMapper.addPerson"; Person person = new Person(4,"laaa",22) ; int count = session.insert(stament, person); session.commit(); System.out.println("增加的人数为"+count); session.close(); } //根据编号删除 public static void deletePersonById() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.PersonMapper.deletePersonById"; int count = session.delete(stament, 2); session.commit(); System.out.println("删除的人数为:"+count); session.close(); } //根据编号更改学生信息 public static void updatePersonById() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.PersonMapper.updatePersonById"; Person person = new Person() ; person.setId(3); person.setAge(27); person.setName("AAAAA"); int count = session.update(stament, person); session.commit(); System.out.println("更新的学生人数为:"+ count); session.close(); } public static void main(String[] args) throws IOException { queryPersonById(); queryAllPerson(); updatePersonById(); queryAllPerson(); //addPerson(); //deletePersonById(); } }
二 mapper动态代理方式的crud(mybatis接口开发)
原则:约定优于配置
硬编码方式:abc.xml
Configuration conf = new Configuration();
con.serName("myProject") ;
配置方式:abc.xml
<name>myProjec</name>
约定:默认值就是myProjec
动态代理具体实现步骤:
建一个mapper接口,遵循以下约定
/*
* 1. 方法名和mapper.xml文件中的标签id值相同
* 2. 方法的输入参数和mapper.xml文件中的标签的parameterType类型一致
* 3. 方法的返回值和mapper.xml文件中标签的resultType类型一致
*/
除以上约定,要实现接口中的方法和mapper.xml中SQL标签一一对应,:
namespace的值就是接口的全类名(接口-mapper.xml一一对应),匹配过程:
根据接口名找到mapper.xml文件(根据的是namespace=接口全类名)
根据接口方法名找到mapper.xml文件中的SQL标签(方法名=SQL标签的ID值)
习惯:
SQL映射文件(mapper.xml)和接口放在同一个包中(注意修改config。xml中加载映射文件的路径)
package com.liusong.test; 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 com.liusong.entity.Student; import com.liusong.mapper.StudentMapper; public class TestDemo { /* * //查询单个学生 public static void queryStudentById() throws IOException { //将config.xml变为流 Reader reader = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.entity.StudentMapper.queryStudentById"; Student student = session.selectOne(stament, 1); System.out.println("根据编号查询到的学生为:"+student); session.close(); } */ public static void queryStudentById() throws IOException { //将config.xml变为流 Reader reader = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.mapper.StudentMapper.queryStudentById"; StudentMapper studentMapper = session.getMapper(StudentMapper.class) ; Student student = studentMapper.queryStudentById(1) ; System.out.println("根据编号查询到的学生为:"+student); session.close() ; } //查询所有学生 public static void queryAllPerson() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.mapper.StudentMapper.queryAllStudent"; StudentMapper studentMapper = session.getMapper(StudentMapper.class); List<Student> students = studentMapper.queryAllStudent() ; System.out.println("查询到所有学生信息为:"+students); session.close(); } //增加学生 public static void addStudent() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.mapper.StudentMapper.addStudent"; StudentMapper studentMapper = session.getMapper(StudentMapper.class); Student student = new Student() ; student.setId(3); student.setName("eeeeee"); student.setAge(22); studentMapper.addStudent(student); session.commit(); System.out.println("添加学生成功!"); session.close(); } //根据编号删除 public static void deleteStudentById() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.mapper.StudentMapper.deleteStudentById"; StudentMapper studentMapper = session.getMapper(StudentMapper.class) ; studentMapper.deleteStudentById(3); session.commit(); System.out.println("删除成功!"); session.close(); } //根据编号更改学生信息 public static void updateStudentById() throws IOException { //将config.xml变为流 Reader reader1 = Resources.getResourceAsReader("conf.xml"); //创建sessionFactory对象,sessionFactory是一个接口,不能new,需要用到SqlSessionFactoryBuilder,并使用其中的build(Reader reader)方法 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader1); SqlSession session = sessionFactory.openSession(); String stament = "com.liusong.mapper.StudentMapper.updateStudentById"; Student student = new Student() ; student.setId(3); student.setAge(20); student.setName("BBBBB"); int count = session.update(stament, student); session.commit(); System.out.println("更新的学生人数为:"+ count); session.close(); } public static void main(String[] args) throws IOException { queryStudentById() ; queryAllPerson(); addStudent(); queryAllPerson(); deleteStudentById(); queryAllPerson(); updateStudentById(); queryAllPerson(); } }