准备工作:1.创建一个maven工程,然后将pojo类导入到项目中去。
2.导入依赖到pom.xml文件中
3.在resources中创建log4j.properites和mybatis-config.xml
4.创建UserMapper接口和UserMapper.xml文件
5.创建测试类MyBatisTest
1.创建Maven工程,还没有配置Maven的和还不会的去看这里啦:maven的下载安装与配置环境变量!!!(全网最详细)-CSDN博客
2.导入依赖到pom.xml文件中
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
3.在resources中创建log4j.properites和mybatis-config.xml
log4j.properites:
# 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
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>
<!--使用dev环境-->
<environments default="dev">
<!--dev环境-->
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<!--使用连接池中的数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3305/mybatis?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<!-- 扫描映射文件 -->
<mappers>
<mapper resource="com/by/mapper/UserMapper.xml"/>
</mappers>
</configuration>
4.创建UserMapper接口和UserMapper.xml文件
UserMapper.java:
package com.by.mapper;
import com.by.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
/**
* <p>Project: mybatis - UserMapper</p>
* <p>Powered by scl On 2023-12-19 15:24:18</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public interface UserMapper {
User getUserById(Integer id );
User getUser(Integer id , String username);
User getUser1(@Param("id") Integer id , @Param("username") String username);
User getUser2(User user);
User getUser3(Map<String ,Object> map);
}
UserMapper.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.by.mapper.UserMapper">
<select id="getUserById" resultType="com.by.pojo.User">
select * from user where id=#{id}
</select>
<select id="getUser" resultType="com.by.pojo.User">
<!--select * from user where id=#{arg0} and username=#{arg1};-->
select * from user where id=#{param1} and username=#{param2};
</select>
<select id="getUser1" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username}
</select>
<select id="getUser2" parameterType="com.by.pojo.User" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username}
</select>
<select id="getUser3" parameterType="java.util.Map" resultType="com.by.pojo.User">
select * from user where id=#{id} and username=#{username}
</select>
</mapper>
5.创建测试类MyBatisTest
/*
* Copyright (c) 2020, 2023, All rights reserved.
*
*/
package com.by;
import com.by.mapper.UserMapper;
import com.by.pojo.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 org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* <p>Project: mybatis - MyBatisTest</p>
* <p>Powered by scl On 2023-12-18 11:44:53</p>
* <p>描述:<p>
*
* @author 孙臣龙 [1846080280@qq.com]
* @version 1.0
* @since 17
*/
public class MyBatisTest {
private InputStream inputStream;
private SqlSession sqlSession;
@Before
public void init() throws IOException {
// 加载配置文件
String resource = "mybatis-config.xml";
inputStream = Resources.getResourceAsStream(resource);
// 创建sqlSessionFActory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获得数据的绘画实例
sqlSession = sessionFactory.openSession();
}
/**
* 传递单个参数
* @throws IOException
*/
@Test
public void testGetById() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(43);
System.out.println(user);
}
/**
* 传递多个参数
* @throws IOException
*/
@Test
public void testGetUser() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUser(43,"俞莲舟");
System.out.println(user);
}
/**
* 注解传递多个参数
* @throws IOException
*/
@Test
public void testGetUser1() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUser(43,"俞莲舟");
System.out.println(user);
}
/**
* 对象传递多个参数
* @throws IOException
*/
@Test
public void testGetUser2() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user1 = new User();
user1.setId(43);
user1.setUsername("俞莲舟");
User user = userMapper.getUser2(user1);
System.out.println(user);
}
/**
* map传递多个参数
* @throws IOException
*/
@Test
public void testGetUser3() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String ,Object> m = new HashMap<>();
m.put("id",43);
m.put("username","俞莲舟");
User user = userMapper.getUser3(m);
System.out.println(user);
}
@After
public void close() throws IOException {
inputStream.close();
sqlSession.close();
}
}