10分钟入门MyBatis--跟我从零开始学MyBatis
1.准备MyBatis环境
1.1 create sql database
create database studentdb; use studentdb; create table student ( id int auto_increment primary key, name varchar(20) unique not null, age int not null, phone varchar(11) not null, address varchar(50) not null ); insert into student values(null,'user1',21,'13311112222','hubeiwuhan'); insert into student values(null,'user2',32,'13311113333','jiangsuchangde'); insert into student values(null,'user3',14,'13314444222','shanghaipudong'); insert into student values(null,'user4',26,'13311555522','beijingdongcheng'); insert into student values(null,'user5',31,'13317777222','wuhanxudong');
1.2 Building SqlSessionFactory from 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> <properties resource="com/ibatis/config/sql.properties" /> <settings> <setting name="cacheEnabled" value="false"/> </settings> <typeAliases> <typeAlias alias="Student" type="com.ibatis.entity.Student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ibatis/entity/maps/studentMapper.xml" /> </mappers> </configuration>
1.3 sql.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/studentdb username=root password=123
1.4 Exploring Mapped SQL Statements
<?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.ibatis.entity.maps.StudentMapper"> <!--查询所有学生 --> <select id="selectAllStudents" resultType="Student"> select * from Student </select> <!-- 根据ID查询学生 --> <select id="selectStudentById" resultType="Student" parameterType="int"> select * from Student where id=#{id} </select> <!-- 添加学生 --> <insert id="addStudent" parameterType="Student"> insert into Student(name,age,phone,address) values (#{name},#{age},#{phone},#{address}) </insert> <!-- 更新学生信息 --> <update id="updateStudent" parameterType="Student"> update Student set name=#{name}, age=#{age}, phone=#{phone}, address=#{address} where id=#{id} </update> <!-- 删除学生信息 --> <delete id="deleteStudent" parameterType="int"> delete from Student where id=#{id} </delete> <!-- 根据学生姓名模糊查询学生 --> <select id="selectStudentByName" parameterType="String" resultType="Student"> select * from Student where name like "%"#{name}"%" </select> </mapper>
2.Start
2.1 building entityBean
package com.ibatis.entity; import java.io.Serializable; @SuppressWarnings("serial") public class Student implements Serializable { private Integer id; private String name; private Integer age; private String phone; private String address; public Student(){ } public Student(String name, Integer age, String phone, String address) { this.name = name; this.age = age; this.phone = phone; this.address = address; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } 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; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "id:" + this.id + " name:" + this.name + " age:" + this.age + " phone:" + this.phone + " address:" + this.address; } }
2.2 building sqlSessionFactory
String resource = "com/ibatis/config/mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession();
2.3 selectAllStudent
List<Student> list=session.selectList("selectAllStudents");
2.4 selectStudentById
Student student = session.selectOne("selectStudentById",id);
2.5 addStudent
this.session.insert("addStudent", student); this.session.commit();
2.6 updateStudent
this.session.update("updateStudent", student); this.session.commit();
2.7 deleteStudent
this.session.delete("deleteStudent", id); this.session.commit();
2.8 selectStudentsByName
List<Student> list=session.selectList("selectStudentByName",name);
2.9 Test