建立第一個MyBatis例項(詳細)
阿新 • • 發佈:2019-02-01
建立第一個MyBatis例項
(1),建立資料庫中的表;
(2),建立專案並新增MyBatis框架所需的jar包;
(3),編寫實體類;
(4),編寫與實體類對應的對映介面(xxxMapper.java),以及對映配置檔案(xxxMapper.xml);
(5),配置MyBatis框架的配置檔案(mybatis-config.xml);
(6),編寫MyBatis框架工具類;
(7),編寫測試應用類並執行;
1,建立表: tb_student
create table TB_STUDENT
(
id int not null,
`name`VARCHAR (50) not null,
score int not null,
PRIMARY KEY(id)
);
2,建立專案
新增所需的jar包到/WEB-INF/lib
3,編寫實體類
編寫與tb_student表對應的實體類Student,程式碼如下
package com.mybatis.pojos;
public class Student {
private int id;
private String name;
private int score;
//必須要有無參的構造方法,不然根據StudentMapper.xml中的配置,在查詢資料庫時,將不能反射構造出Student實列
public Student(){
}
//帶引數的構造方法
public Student(int id,String name,int score){
this.id=id;
this.name=name;
this.score=score;
}
@Override
public String toString(){
return "學號: "+id+"\n姓名: "+name+"\n成績: "+score;
}
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 getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
4,關係對映
編寫與Student實體類所對應的對映介面StudentMapper,該介面是用於資料訪問操作的介面,程式碼如下
package com.mybatis.mapper;
import com.mybatis.pojos.Student;
public interface StudentMapper {
public void insertStudent(Student student);//新增學生物件
public Student getStudent(String name); //查詢學生物件
}
編寫對應的對映配置檔案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">
<mapper namespace="com.mybatis.mapper.StudentMapper">
<insert id="insertStudent" parameterType="Student">
insert into tb_student(id,name,score)values(#{id},#{name},#{score})
</insert>
<!-- 這裡的id必須和StudentMapper介面中的介面方法名相同,不然執行時會報錯 -->
<select id="getStudent" resultType="Student"
parameterType="java.lang.String" >
select id,name,score from tb_student where name=#{name}
</select>
</mapper>
5,配置mybatis-config.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>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="Student" type="com.mybatis.pojos.Student" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<property name="driver"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/hibernateuser" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource = "com/mybatis/mapper/StudentMapper.xml"></mapper>
</mappers>
</configuration>
6,編寫MyBatisUtil工具類
用於獲取SqlSessionFactory物件,程式碼如下
package com.mybatis.util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private final static SqlSessionFactory sqlSessionFactory;
static{
String resource="mybatis-config.xml";
Reader reader=null;
try{
//載入"mybatis-config.xml
reader = Resources.getResourceAsReader(resource);
}catch(IOException e){
System.out.println(e.getMessage());
}
sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
7,編寫測試應用類
package com.mybatis;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.mybatis.mapper.StudentMapper;
import com.mybatis.pojos.Student;
import com.mybatis.util.MyBatisUtil;
public class MyBatisDemo {
static SqlSessionFactory sqlSessionFactory=MyBatisUtil.getSqlSessionFactory();
public static void main(String[] args) {
// //新增4個學生物件
// addStudent(new Student(1,"it",89));
// addStudent(new Student(2,"is",98));
// addStudent(new Student(3,"so",99));
// addStudent(new Student(4,"easy",99));
//根據姓名查詢
Student student=getStudent("niu");
System.out.println(student.toString());
}
//新增學生資訊
public static void addStudent(Student student){
//開啟SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
//獲取Mapper物件
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//插入操作
studentMapper.insertStudent(student);
//提交SqlSession
sqlSession.commit();
System.out.println("新增成功");
}finally{
//關閉sqlSession
sqlSession.close();
}
}
//根據姓名查詢學生資訊
public static Student getStudent(String name){
Student student=null;
//開啟SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
//獲取Mapper物件
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
//查詢操作
student=studentMapper.getStudent(name);
}finally{
//關閉sqlSession
sqlSession.close();
}
return student;
}
}
專案檔案目錄如下: