作者:用户gokk5efqd3 | 来源:互联网 | 2023-06-25 01:12
开启log4j
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
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>
1.定义mapper接口,接口为定义单词方法名
public interface UserMapper {
/**
* MyBatis面向接口编程的两个一致:
* 1、映射文件的namespace要和mapper接口的全类名保持一致
* 2、映射文件中SQL语句的id要和mapper接口中的方法名一致
*
* 表--实体类--mapper接口--映射文件
*/
/**
* 添加用户信息
*/
int insertUser();
/**
* 修改用户信息
*/
void updateUser();
/**
* 删除用户信息
*/
void deleteUser();
/**
* 根据id查询用户信息
*/
User getUserById();
/**
* 查询所有的用户信息
*/
List getAllUser();
}2.定义mapper接口的映射文件,为xxx.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.atguigu.mybatis.mapper.UserMapper">
<insert id="insertUser">
insert into t_user values(null,'admin','123456',23,'男','12345@qq.com')
insert>
<update id="updateUser">
update t_user set username = '张三' where id = 4
update>
<delete id="deleteUser">
delete from t_user where id = 5
delete>
<select id="getUserById" resultType="com.atguigu.mybatis.pojo.User">
select * from t_user where id = 3
select>
<select id="getAllUser" resultType="User">
select * from t_user
select>
mapper>3.配置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="jdbc.properties" />
<typeAliases>
这个只能设置某一个类的别名
<package name="com.atguigu.mybatis.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.atguigu.mybatis.mapper"/>
mappers>
configuration>4.测试
/**
* SqlSession默认不自动提交事务,若需要自动提交事务
* 可以使用SqlSessionFactory.openSession(true);
*/
@Test
public void testMyBatis() throws IOException {
//加载核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessiOnFactoryBuilder= new SqlSessionFactoryBuilder();
//获取sqlSessionFactory
SqlSessionFactory sqlSessiOnFactory= sqlSessionFactoryBuilder.build(is);
//获取SqlSession opensession设置自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//获取mapper接口对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//测试功能
int result = mapper.insertUser();
//提交事务
//sqlSession.commit();
System.out.println("result:"+result);
}
在核心配置文件中 设置别名和mapper的映射 在springboot中都是中在yml文件中配置的,所以只需要记mapper接口和mapper映射关系,和resulttype和resultmap这个是要详细记录的,然后还有参数