使用Maven创建MyBatis(超详细 IDEA 2020版)
程序员文章站
2022-04-15 18:38:45
在pom.xml中配置需要的Jar包 junit junit 4.9 ......
注意:
数据库自备
自己改链接数据库配置文件(mybatis-config)
文章目录
创建Mybatis
在Maven原有的基础上创建一个Mybatis
File --> New --> project
起项目名字
Maven目录结构
打开pom.xml文件
将需要的Jar包写入在红色方框里,配置文件如下:
<dependencies>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!-- 整合log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
导入依赖
配置文件
在/src/resource文件里分别创建
EmpMapper.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.dao.EmpMapper">
这里编写sql语句
</mapper>
mybatis-config(sql连接池、导入sql语句)
<?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 >
<!-- 配置开发环境 -->
<environments default="develop">
<environment id="develop">
<!-- 1.1指定事务管理 将事物交给JDBC来管理 -->
<transactionManager type="JDBC"/>
<!-- 1.2datasource(数据源/连接池,)指定链接数据库的基本信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///yonghedb?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 导入mapper文件(sql语句) -->
<mappers>
<mapper resource="EmpMapper.xml"/>
</mappers>
</configuration>
log4j.properties(输出日志优化)
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
创建类
在java文件夹中:
XXXMapper(接口映射sql)
package com.dao;
import java.util.List;
import com.pojo.Emp;
/*接口的全类名:*/
public interface EmpMapper {
public List<Emp> findAll01();
// public int insert06(Map map);
public int insert06(Emp emp);
}
XXX(返回映射sql)
package com.pojo;
public class Emp {
private Integer id;
private String name;
private String job;
private Double salary;
// 提供有参构造方法,记得无参构造也带上
// 提供get和set方法
public Integer getId() {
return id;
}
public Emp() {
super();
}
public void setId(Integer id) {
System.out.println("封装执行了.."+id);
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
// 提供tostring方法
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", job=" + job + ", salary=" + salary + "]";
}
}
test(运行类)
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.Before;
import org.junit.Test;
import com.dao.EmpMapper;
import com.pojo.Emp;
public class MyBatisTest01 {
private SqlSession session =null;
@Before/*注解标记的方法会在@Test注解标记的方法之前执行*/
public void testBefore() throws IOException {
//读取mybatis核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
// 获取SqlSessionFactory工厂对象
SqlSessionFactory fac =
new SqlSessionFactoryBuilder().build(in);
// 获取sqlsesssion对象
session=fac.openSession(true);//true表示自动提交事物,false表示手动提交事务
}
/*接口开发*/
@Test
public void testFindAll01() {
EmpMapper mapper= session.getMapper(EmpMapper.class);
List<Emp> list = mapper.findAll01();
for(Emp emp:list) {
System.out.println(emp);
}
}
@Test
public void testInesrt02() {
EmpMapper mapper=session.getMapper(EmpMapper.class);
// Map map = new HashMap();
// map.put("name","马云");
// map.put("job","教师");
// map.put("salary","1000");
Emp emp = new Emp();
emp.setName("马云");
emp.setJob("阿里巴巴ceo");
emp.setSalary(9999.0);
mapper.insert06(emp);
}
}
运行:
本文地址:https://blog.csdn.net/weixin_46062010/article/details/109256392