2.1 开发环境的准备
- 导入 MyBatis 框架的 jar 包、Mysql 驱动包、log4j 的 jar 包
- 导入log4j的配置文件
log4j.rootLogger=DEBUG,Console,A
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.A = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A.File = D:/log.txt
log4j.appender.A.Append = true
log4j.appender.A.Threshold = DEBUG
log4j.appender.A.layout = org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.A.encoding=UTF-8
2.2 创建测试表
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE `t_employee` (
`id` INT (11) NOT NULL AUTO_INCREMENT,
`empname` VARCHAR (50) NOT NULL,
`gender` INT (1) DEFAULT NULL,
`email` VARCHAR (100) DEFAULT NULL,
`login_account` VARCHAR (100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE = INNODB AUTO_INCREMENT = 13 DEFAULT CHARSET = utf8
- 创建成功的表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aebb8c9bebf42d6f4bd19d5d8731648c.png)
2.3 IEDA目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d06edb665089bc5c309d866e60efbf3e.png)
2.4 创JavaBean机和Mapper接口
public class Employee {
private Integer id;
private String empName;
private Integer gender;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer 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 +
", empName='" + empName + '\'' +
", gender=" + gender +
", email='" + email + '\'' +
'}';
}
}
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
public int updateEmployee(Employee employee);
public int deleteEmployee(Integer id);
public int insertEmployee(Employee employee);
}
2.5 创建mybatis全局配置文件
<?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="db.properties"></properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<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/mybatis?useSSL=false"/>
<property name="username" value="数据库登录用户名"/>
<property name="password" value="登录密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml"/>
</mappers>
</configuration>
2.6 创建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.wolffy.dao.EmployeeMapper">
<select id="getEmpById" resultType="com.wolffy.pojo.Employee">
select * from t_employee where id = #{id}
</select>
<update id="updateEmployee">
update t_employee
set empname= #{empName}, gender= #{gender}, email= #{email}
where id= #{id}
</update>
<delete id="deleteEmployee" parameterType="int">
delete from t_employee where id= #{id}
</delete>
<insert id="insertEmployee">
insert into t_employee (empname, gender, email)
values (#{empName}, #{gender}, #{email});
</insert>
</mapper>
2.7 测试
**
* @author wolffy
* @create 2021-06-15 21:27
*/
public class MybatisTest {
SqlSessionFactory sqlSessionFactory;
SqlSession sqlSession;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
}
@Test
public void test() throws IOException {
SqlSession sqlSession = sqlSessionFactory.openSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
try {
Employee employee = employeeMapper.getEmpById(1);
System.out.println(employee);
} finally {
sqlSession.close();
}
}
@Test
public void testInsert() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
int i = mapper.insertEmployee(new Employee("Tomcat",0, "zzz@qq.com"));
System.out.println("添加成功"+ i +"条数据");
} finally {
sqlSession.commit();
sqlSession.close();
}
}
@Test
public void testDelete() {
sqlSession = sqlSessionFactory.openSession();
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
int i = mapper.deleteEmployee(12);
if (i > 0) {
System.out.println("删除" + i + "成功");
} else {
System.out.println("删除失败");
}
}
@After
public void destroy() {
sqlSession.commit();
sqlSession.close();
}
}