本次環(huán)境MySQL5.7.20+MyBatis3.4.1+JDK9.0,用到的插件有:
1.創(chuàng)建一個數(shù)據(jù)庫mybatis和表格tbl_employee:
創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、三河網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為三河等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
CREATE DATABASE mybatis DEFAULT CHARSET utf8;
USE mybatis;
CREATE TABLE tbl_employee1(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
);
2.創(chuàng)建一個MyBatis java工程,編寫與數(shù)據(jù)庫中對應(yīng)字段的Employee類:
package com.atguigu.mybatis.bean;
import org.apache.ibatis.type.Alias;
@Alias("emp")
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String email, String gender) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
}
}
3.創(chuàng)建EmployeeMapper接口,添加增刪改查方法,此接口中的方法可以與映射到xml文件
package com.atguigu.mybatis.dao;
import com.atguigu.mybatis.bean.Employee;
public interface EmployeeMapper {
//查詢
public Employee getEmpById(Integer id);
//插入
public Long addEmp(Employee employee);
//修改
public boolean updateEmp(Employee employee);
//刪除
public void deleteEmpById(Integer id);
}
4.創(chuàng)建dbconfig.properties文件,存放的是JDBC信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.0.0.40:3306/mybatis
jdbc.username=chendan
jdbc.password=123456
5.創(chuàng)建mybatis-config.xml主配置文件,定義了數(shù)據(jù)庫相關(guān)信息
6.創(chuàng)建EmployeeMapper.xml映射文件,存放的主要是SQL語句:
insert into tbl_employee1(last_name,email,gender)
values(#{lastName},#{email},#{gender})
update tbl_employee1
set last_name=#{lastName},email=#{email},gender=#{gender}
where id=#{id}
delete from tbl_employee1 where id=#{id}
7.創(chuàng)建測試類,主要用于JUnit測試使用
package com.atguigu.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
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 org.junit.Test;
import com.atguigu.mybatis.bean.Employee;
import com.atguigu.mybatis.dao.EmployeeMapper;
public class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test01() throws IOException {
// 1、獲取sqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// 2、獲取sqlSession對象
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3、獲取接口的實現(xiàn)類對象
// 會為接口自動的創(chuàng)建一個代理對象,代理對象去執(zhí)行增刪改查方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(mapper.getClass());
System.out.println(employee);
} finally {
openSession.close();
}
}
@Test
public void test02() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = new Employee(null,"Jack","Jack@atguigu.com","0");
mapper.addEmp(employee);
// Employee employee = new Employee(5,"Jack","Jack@atguigu.com","1");
// boolean isupdate = mapper.updateEmp(employee);
// System.out.println(isupdate);
// mapper.deleteEmpById(4);
openSession.commit();
} finally {
openSession.close();
}
}
}
運行結(jié)果如下:
1、執(zhí)行查詢操作,可以看到數(shù)據(jù)庫中只有一條數(shù)據(jù):
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
Employee [id=1, lastName=Jack, email=Jack@atguigu.com, gender=0]
查詢成功
2、執(zhí)行增加一條數(shù)據(jù),執(zhí)行查詢結(jié)果如下:
Employee employee = new Employee(null,"Peter","Peter@atguigu.com","1");
mapper.addEmp(employee);
openSession.commit();
Employee employee = mapper.getEmpById(2);
System.out.println(employee);
DEBUG 08-01 15:11:13,512 <== Updates: 1 (BaseJdbcLogger.java:145)
Employee [id=2, lastName=Peter, email=Peter@atguigu.com, gender=1]
添加成功
3.修改數(shù)據(jù)庫中的數(shù)據(jù),執(zhí)行結(jié)果如下:
Employee employee = new Employee(2,"Jack","Jack@atguigu.com","1");
boolean isupdate = mapper.updateEmp(employee);
openSession.commit();
Employee employee = mapper.getEmpById(2);
System.out.println(employee);
DEBUG 08-01 15:18:02,625 <== Updates: 1 (BaseJdbcLogger.java:145)
Employee [id=2, lastName=Jack, email=Jack@atguigu.com, gender=1]
修改成功
4.刪除數(shù)據(jù)庫中的數(shù)據(jù),執(zhí)行結(jié)果如下:
mapper.deleteEmpById(2);
openSession.commit();
Employee employee = mapper.getEmpById(2);
System.out.println(employee);
DEBUG 08-01 15:23:09,434 <== Updates: 1 (BaseJdbcLogger.java:145)
null
查詢數(shù)據(jù)刪除成功
總結(jié):MyBatis操作數(shù)據(jù)庫簡單快捷,將代碼與sql語句分離出來,非常好!