此处不选用骨架直接next
设置你的mybatis项目的名字以及路径,点击finished
导入测试的数据库
选择自动导入坐标
写入所需要的jar包的坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis_test_enviorment</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<!--mybatis包-->
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<!--测试包-->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<!--日志包-->
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<!--数据库连接包-->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
导入log4j.properties
在java包中创建domain 和 dao 包
在daomain
包中创建一个User
对象,其中的属性与数据库的中列的名字相同
(此处先讲 封装类的属性 与 数据库 列名相同
的情况)
package com.xiao.domain;
import java.io.Serializable;
import java.util.Date;
/**
* @_PackageName:com.xiao.domain
* @_ClassName:User
* @_Description:
* @_Author:笑老二
* @_data 2020/4/16 18:27
*/
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User() {
}
public User(Integer id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
}
创建UserDao接口,其中定义个findAll方法
package com.xiao.dao;
import com.xiao.domain.User;
import java.util.List;
public interface UserDao {
/**
* 查询所有用户
* @return
*/
List<User> findAll();
}
编写 持久层接口的件 映射文件 UserDao.xml
注意:
写入内容
配置mybatis的环境文件SqlMapConfig.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>
<!--配置mybatis的环境-->
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<!--告知mybatis映射配置文件位置-->
<mappers>
<mapper resource="com/xiao/dao/UserDao.xml"/>
</mappers>
</configuration>
创建一个在test文件夹下写入mybatisTest.java进行测试
结构图
package com.xiao.test;
import com.xiao.dao.UserDao;
import com.xiao.domain.User;
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 java.io.InputStream;
import java.util.List;
/**
* @_PackageName:com.xiao.test
* @_ClassName:MybatisTest
* @_Description:
* @_Author:笑老二
* @_data 2020/4/19 18:19
*/
public class MybatisTest {
public static void main(String[] args) throws Exception{
//1.加载配置文件进内存
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建一个工场生产的工具
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.通过工程生产工具生产一个工厂对象
SqlSessionFactory build = builder.build(is);
//4.创建一个sqlSeesion对象
SqlSession sqlSession = build.openSession();
//5.强化类,创建代理对象,来创建实现类
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
//6.关闭流
sqlSession.close();
is.close();
}
}
省略以上重复的创建步骤
在dao包下创建一个impl包放入UserDaoImpl
写入的代码
package com.xiao.dao.impl;
import com.xiao.dao.UserDao;
import com.xiao.domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
/**
* @_PackageName:com.xiao.dao.impl
* @_ClassName:UserDaoImpl
* @_Description:
* @_Author:笑老二
* @_data 2020/4/19 18:29
*/
public class UserDaoImpl implements UserDao {
private SqlSessionFactory factory;
private SqlSession sqlSession;
//强制必须传入factory工厂对象
public UserDaoImpl(SqlSessionFactory factory) {
this.factory = factory;
}
public List<User> findAll() {
sqlSession = factory.openSession();
UserDao u = sqlSession.getMapper(UserDao.class);
List<User> users = u.findAll();
sqlSession.close();
return users;
}
}
mybatisTest代码
package com.xiao.test;
import com.xiao.dao.UserDao;
import com.xiao.dao.impl.UserDaoImpl;
import com.xiao.domain.User;
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 java.io.InputStream;
import java.util.List;
/**
* @_PackageName:com.xiao.test
* @_ClassName:MybatisTest
* @_Description:
* @_Author:笑老二
* @_data 2020/4/19 18:19
*/
public class MybatisTest {
public static void main(String[] args) throws Exception{
//1.加载配置文件进内存
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建一个工场生产的工具
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.通过工程生产工具生产一个工厂对象
SqlSessionFactory factory = builder.build(is);
UserDao userDao = new UserDaoImpl(factory);
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
//6.关闭流
is.close();
}
}