1.项目的相关配置文件
(1)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="jdbc.properties"></properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.shen.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>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml" />
</mappers>
</configuration>
(2)jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=true
jdbc.username=root
jdbc.password=123456
2.CRUD的写法
(1)实体类 Employee.java
package com.shen.bean;
public class Employee {
private Integer id;
private String lastName;
private String gender;
private String email;
public Employee(Integer id, String lastName, String gender, String email) {
super();
this.id = id;
this.lastName = lastName;
this.gender = gender;
this.email = email;
}
public Employee() {
super();
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", gender="
+ gender + ", email=" + email + "]";
}
}
(2)数据库表
CREATE TABLE tbl_employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
)
(3) 接口文件 EmployeeMapper.java
package com.shen.dao;
import org.apache.ibatis.annotations.Param;
import com.shen.bean.Employee;
public interface EmployeeMapper {
public Employee selectEmpByIdAndLastName(@Param("id")Integer id,@Param("lastName")String lastName);
public Employee getEmployeeById(Integer id);
public Integer insertEmployee(Employee employee);
public Boolean deleteEmployee(Integer id);
public Boolean updateEmployee(Employee employee);
}
(4)映射配置文件 EmployeeMapper.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.shen.dao.EmployeeMapper">
<select id="selectEmpByIdAndLastName" resultType="employee">
select * from tbl_employee where id=#{id} and last_name=#{lastName}
</select>
<select id="getEmployeeById" resultType="employee">
select * from tbl_employee where id=#{id}
</select>
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="id">
insert into tbl_employee(last_name,email,gender)
values(#{lastName},#{email},#{gender})
</insert>
<update id="updateEmployee">
update tbl_employee set
last_name=#{lastName},email=#{email},gender=#{gender}
where id=#{id}
</update>
<delete id="deleteEmployee">
delete from tbl_employee where
id=#{id}
</delete>
</mapper>
(5)测试类 CRUD.java
package com.shen.handler;
import java.io.IOException;
import java.io.InputStream;
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.shen.bean.Employee;
import com.shen.dao.EmployeeMapper;
public class CRUD {
/**
* 测试曾删改查
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println(selectEmpByIdAndLastName(2,"yu"));
}
private static SqlSession getMySession() throws IOException{
String resource ="mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession();
}
public static Employee selectEmp(Integer id) throws IOException {
SqlSession session = getMySession();
Employee employee ;
try{
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
employee = mapper.getEmployeeById(id);
}finally{
session.close();
}
return employee;
}
public static void insertEmp(Employee employee) throws IOException {
SqlSession session = getMySession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Integer ressult = mapper.insertEmployee(employee);
System.out.println(employee);
session.commit();
} finally {
session.close();
}
}
public static void updateEmp(Employee employee) throws IOException {
SqlSession session = getMySession();
try{
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
employee.setLastName("chao");
Boolean result = mapper.updateEmployee(employee);
System.out.println(result);
session.commit();
}finally{
session.close();
}
}
public static void deleteEmp(Integer id) throws IOException {
SqlSession session = getMySession();
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Boolean result = mapper.deleteEmployee(id);
System.out.println(result);
session.commit();
} finally {
session.close();
}
}
/**
* 当传入多个参数的时候,mybatis的处理方式
* */
public static Employee selectEmpByIdAndLastName(Integer id,String lastName) throws IOException {
SqlSession session = getMySession();
Employee employee;
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
employee = mapper.getEmployeeById(id);
} finally {
session.close();
}
return employee;
}
}