文章目录
- 1.新建SqlSessionUtils工具类
- 2.编写静态方法
- 3.项目结构及代码
- 项目结构
- 数据库和表
- pom.xml
- ParameterMapper接口:
- User类:
- ParameterMapper.xml
- jdbc.properties
- log4j.xml:
- mybatis-config.xml:
- ParameterMapperTest测试类:
- 测试结果
1.新建SqlSessionUtils工具类
2.编写静态方法
SqlSessionUtils类:
package com.rqs.mybatis.utils;
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.IOException;
import java.io.InputStream;
public class SqlSessionUtils {
public static SqlSession getSqlSession() {
SqlSession sqlSession = null;
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
}
3.项目结构及代码
项目结构
数据库和表
pom.xml
<?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>com.rqs.mybatis</groupId>
<artifactId>MyBatis_demo2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Mybatis核心 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- junit测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
ParameterMapper接口:
package com.rqs.mybatis.mapper;
import com.rqs.mybatis.pojo.User;
import java.util.List;
public interface ParameterMapper {
/**
* 查询所有员工信息
*/
List<User> getAllUser();
}
User类:
package com.rqs.mybatis.pojo;
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
private String email;
public User() {
}
public User(Integer id, String username, String password, Integer age, String gender, String email) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.gender = gender;
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
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;
}
}
ParameterMapper.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.rqs.mybatis.mapper.ParameterMapper">
<!-- List<User> getAllUser();-->
<select id="getAllUser" resultType="user">
select * from t_user
</select>
</mapper>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS}
%m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
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>
<!-- MyBatis核心配置文件,标签的顺序
properties?,settings?,typeAliases?,typeHandlers?,
objectFactory?,objectWrapperFactory?,reflectorFactory?,
plugins?,environments?,databaseIdProvider?,mappers?-->
<properties resource="jdbc.properties"></properties>
<!--设置类型别名,大小写不敏感。
如果不设置alias,则默认为类名(大小写不敏感)-->
<typeAliases>
<!--
typeAlias: 设置某个类型的别名
属性:
type 设置需要设置别名的类型
alias 设置某个类型的别名,如果不设置该属性,那么该类型拥有默认的类名,且不区分大小写
-->
<!--<typeAlias type="com.rqs.mybatis.pojo.User" alias="User"></typeAlias>-->
<!--推荐以包为单位,将包下所有的类型设置默认的类型别名且不区分大小写-->
<package name="com.rqs.mybatis.pojo"/>
</typeAliases>
<!--设置连接数据库的环境-->
<!--每一个environment都是具体连接数据库的环境-->
<!--
一个项目中只会用一个环境,default用于使用默认使用的环境:
id:表示连接数据库的环境的唯一标识 不能重复
-->
<environments default="development">
<!--
transactionmanager:设置事务管理方式
属性:
type="JDBC/MANAGED"
JDBC: 在当前环境中,执行sql时,使用的时jdbc原声的事务管理方式,需要手动的提交和回滚事务
MANAGED:被管理,例如Spring
-->
<environment id="development">
<transactionManager type="JDBC"/>
<!-- dataSource:配置数据源
属性"
type:设置数据源的类型
type=""
POOLED:表示使用数据库连接池缓存数据库连接
UNPOOLED:表示不使用数据库连接池
JNDI:表示使用上下文中的数据源
-->
<dataSource type="POOLED">
<!--设置连接数据库的驱动-->
<property name="driver" value="${jdbc.driver}"/>
<!--设置连接地址-->
<property name="url" value="${jdbc.url}"/>
<!--注意:如果在建sql表单的时候选了字符集(如utf8),
这里的value要改成:value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"-->
<!--用户名和密码-->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<!--
推荐以包为单位引入映射文件,要求:
1。 mapper接口所在的包要和映射文件所在的包一致
2。 mapper接口要和映射文件的名字一致-->
<!-- com.rqs.mybatis.mapper创建包时要用/分隔,这样才是目录,否则这整一个就只是文件夹名字而已-->
<package name="com.rqs.mybatis.mapper"/>
</mappers>
</configuration>
ParameterMapperTest测试类:
package com.rqs.mybatis.test;
import com.rqs.mybatis.mapper.ParameterMapper;
import com.rqs.mybatis.pojo.User;
import com.rqs.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class ParameterMapperTest {
@Test
public void getAllUser() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
List<User> allUser = mapper.getAllUser();
for(int i=0; i<allUser.size(); i++){
System.out.println(allUser.get(i));
}
}
}