您好,登錄后才能下訂單哦!
本次環境MySQL5.7.20+MyBatis3.4.1+JDK9.0,用到的插件有:
1.創建一個數據庫mybatis和表格tbl_employee:
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.創建一個MyBatis java工程,編寫與數據庫中對應字段的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.創建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.創建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.創建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>
<properties resource="dbconfig.properties"></properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!-- typeAlias type="com.atguigu.mybatis.bean.Employee" alias="emp"/>-->
<package name="com.atguigu.mybatis.bean"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
<environment id="test">
<transactionManager type="test" />
<dataSource type="POOLED">
<property name="driver" value="${orcl.driver}" />
<property name="url" value="${orcl.url}" />
<property name="username" value="${orcl.username}" />
<property name="password" value="${orcl.password}" />
</dataSource>
</environment>
</environments>
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
<property name="SQL Server" value="sql server"/>
</databaseIdProvider>
<!-- 將我們寫好的sql映射文件(EmployeeMapper.xml)一定要注冊到全局配置文件(mybatis-config.xml)中 -->
<mappers>
<!-- <mapper resource="EmployeeMapper2.xml" /> -->
<package name="com.atguigu.mybatis.dao"/>
</mappers>
</configuration>
6.創建EmployeeMapper.xml映射文件,存放的主要是SQL語句:
<?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.atguigu.mybatis.dao.EmployeeMapper">
<!--public Employee getEmpById(Integer id); 查詢操作 -->
<select id="getEmpById"
resultType="com.atguigu.mybatis.bean.Employee" databaseId="mysql">
select * from tbl_employee1 where id = #{id}
</select>
<select id="getEmpById"
resultType="com.atguigu.mybatis.bean.Employee" databaseId="oracle">
select
EMPLOYEE_ID id,LAST_NAME lastName,EMAIL email from employees where
EMPLOYEE_ID = #{id}
</select>
<!-- 插入操作 public void addEmp(Employee employee);-->
<insert id="addEmp" parameterType="com.atguigu.mybatis.bean.Employee">
insert into tbl_employee1(last_name,email,gender)
values(#{lastName},#{email},#{gender})
</insert>
<!-- 更新操作 public void updateEmp(Employee employee); -->
<update id="updateEmp">
update tbl_employee1
set last_name=#{lastName},email=#{email},gender=#{gender}
where id=#{id}
</update>
<!-- 刪除操作 public void deleteEmpById(Integer id);-->
<delete id="deleteEmpById">
delete from tbl_employee1 where id=#{id}
</delete>
</mapper>
7.創建測試類,主要用于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、獲取接口的實現類對象
// 會為接口自動的創建一個代理對象,代理對象去執行增刪改查方法
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();
}
}
}
運行結果如下:
1、執行查詢操作,可以看到數據庫中只有一條數據:
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
Employee [id=1, lastName=Jack, email=Jack@atguigu.com, gender=0]
查詢成功
2、執行增加一條數據,執行查詢結果如下:
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.修改數據庫中的數據,執行結果如下:
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.刪除數據庫中的數據,執行結果如下:
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
查詢數據刪除成功
總結:MyBatis操作數據庫簡單快捷,將代碼與sql語句分離出來,非常好!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。