<?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>
<!-- 引入 jdbc.properties 文件,尔后能够应用 ${key} 的形式来拜访 value-->
<properties resource="jdbc.properties"/>
<!--
设置别名的标签, 为某个具体的类型来设置一个别名,在 mybatis 配置文件的范畴中,来示意一个具体的类型
mybatis 外围配置文件必须按指定的程序配置
type="" 须要起别名的类型
alias 示意别名的名字
-->
<typeAliases>
<!--<typeAlias type="com.huawei.mybatis.pojo.User" alias="uerMapperAliase"></typeAlias>-->
<!-- 或者 若不设置 alias,以后的别名领有类型的别名,即类名且不辨别大小写 -->
<!-- <typeAlias type="com.huawei.mybatis.pojo.User"></typeAlias>-->
<!-- 以包的形式来写别名 -->
<!-- 通过包来设置类型别名,指定包下所有的类型领有默认的别名,即类名且不辨别大小写 -->
<package name="com.huawei.mybatis.pojo"/>
</typeAliases>
<!--
配置连贯数据库的环境
default="" 设置默认应用环境的 id
-->
<environments default="development">
<!--
environment 设置一个具体的连贯数据库的环境
id 设置环境的惟一标识不能反复
-->
<environment id="development">
<!--
transactionManager 设置事务管理器
type 设置事务的治理形式
type="JDBC" 应用 JDBC 原生的事务管理形式
type="MANAGED" 示意被治理,例如 Spring
-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--
dataSource 设置数据源
type="" 示意设置数据源的类型
type="POOLED" 示意应用数据库连接池
type="UNPOOLED" 示意不应用数据库连接池,每次连贯从新创立连接池
type="JNDI" 示意应用上下文的数据源
-->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jsbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 引入 mybatis 的映射文件 -->
<mappers>
<!--<mapper resource="mappers/UserMapper.xml"/>-->
<!--
通过包来引入映射文件,必须满足两个条件:1.mapper 接口和映射文件所在的包必须统一
2.mapper 接口的名字和映射文件的名字必须统一
-->
<package name="com.huawei.mybatis.mapper"/>
</mappers>
</configuration>
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jsbc.username=root
jdbc.password=123456
package com.huawei.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;
}
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;}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
}
package com.huawei.mybatis.mapper;
import com.huawei.mybatis.pojo.User;
import java.util.List;
public interface UserMapper {
/**
* 增加用户信息
* @return
*/
int insertUser();
/**
* 批改用户信息
* @return
*/
int updateUser();
/**
* 删除用户信息
*/
void deleteUser();
/**
* 依据用户 id 查问用户信息
*/
User getUserById();
/**
* 查问所有用户信息
*/
List<User> getAllUser();}
<?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.huawei.mybatis.mapper.UserMapper">
<!--
mapper 接口和映射文件要保持一致:1.mapper 接口的全类名要和 namespace 保持一致
2.mapper 接口的办法名要和映射文件的 id 保持一致
-->
<!-- int insertUser(); -->
<insert id="insertUser">
insert into t_user values(null,'admin','123','23','男','[email protected]')
</insert>
<!--int updateUser();-->
<update id="updateUser">
update t_user set username='zhj',password='456' where id = 3
</update>
<!--void deleteUser();-->
<delete id="deleteUser">
delete from t_user where id = 3
</delete>
<!--User getUserById();-->
<!--
resultType 设置后果类型,查问的后果要转换的 Java 类型
resuleMap 自定义类型,解决一对多或多对一的映射关系
-->
<select id="getUserById" resultType="com.huawei.mybatis.pojo.User">
select * from t_user where id = 2
</select>
<!--List<User> getAllUser();-->
<!--<select id="getAllUser" resultType="com.huawei.mybatis.pojo.User">-->
<!-- 应用别名的形式 -->
<!--<select id="getAllUser" resultType="uerMapperAliase">-->
<!-- 不写 alias 应用不辨别大小写类型来做别名 -->
<select id="getAllUser" resultType="User">
select * from t_user
</select>
</mapper>
package com.huawei.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 SqlSessionUtil {public static SqlSession getSqlSession(){
SqlSession sqlSession= null;
try {
// 获取外围配置文件的输出流
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
// 获取 sqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 获取 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 获取
sqlSession = sqlSessionFactory.openSession(true);
} catch (IOException e) {e.printStackTrace();
}
return sqlSession;
}
}
<?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>
package com.huawei.mybatis.test;
import com.huawei.mybatis.mapper.UserMapper;
import com.huawei.mybatis.pojo.User;
import com.huawei.mybatis.utils.SqlSessionUtil;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MybatisTest {
@Test
public void testInsert() throws IOException {
// 获取外围配置文件的输出流
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
// 获取执行 sql 语句的对象 sqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 获取 sqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
// 获取 sql 的会话对象 sqlSession,是 mybatis 提供的操作数据库的对象
// 设置主动提交事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);
// SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取 UserMapper 的代理实现类对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 调用 userMapper 接口的办法,实现用户新增的性能
int result = userMapper.insertUser();
System.out.println("后果:"+result);
// 这种实现形式须要本人提交事务
// sqlSession.commit();
// 敞开 sqlSession 的会话
sqlSession.close();}
@Test
public void testUpdate(){SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser();
sqlSession.close();}
@Test
public void deleteUpdate(){SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser();
sqlSession.close();}
@Test
public void testGetUserById(){SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById();
System.out.println(user);
}
@Test
public void testGetAllUser(){SqlSession sqlSession = SqlSessionUtil.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getAllUser();
// userList.forEach(x->{System.out.println(x);});
userList.forEach(System.out::println);
System.out.println("后果:" + userList);
}
}