<dependencies>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 添加MySQL的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependencies>
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
package com.bobo.pojo;
/**
* 多列操作数据
* 按住Alt键 鼠标操作
*/
public class User {
private Integer id;
private String username;
private String address;
private String gender ;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public User() {
}
public User(Integer id, String username, String address, String gender) {
this.id = id;
this.username = username;
this.address = address;
this.gender = gender;
}
public User(String username, String address, String gender) {
this.username = username;
this.address = address;
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
<?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.bobo.pojo.User">
<!-- 添加用户 -->
<insert id="addUser" parameterType="com.bobo.pojo.User" >
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
</insert>
</mapper>
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/shop?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 关联我们自定义的映射文件 -->
<mapper resource="com/bobo/pojo/UserMapper.xml"/>
</mappers>
</configuration>
? ? ?
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
}
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
jdbc:mysql://localhost:3306/shop?
serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
// 5.MyBatis中默认是不会自动提交DML操作的数据的
sqlSession.commit();
// 6.关闭会话
sqlSession.commit();
}
? ? ? ?
<?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.bobo.pojo.User">
<!-- 添加用户 -->
<insert id="addUser" parameterType="com.bobo.pojo.User" >
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
</insert>
<!-- 更新用户信息 -->
<update id="updateUser" parameterType="com.bobo.pojo.User">
update t_user set username=#{username} ,address=#{address},gender=#{gender} where id =#{id}
</update>
<!-- 删除用户信息 -->
<delete id="deleteUser" >
delete from t_user where id=#{id}
</delete>
<!-- 查询所有用户信息 -->
<select id="queryAll" resultType="com.bobo.pojo.User">
select * from t_user
</select>
<!-- 根据用户编号查询用户信息 -->
<select id="queryById" parameterType="java.lang.Integer" resultType="com.bobo.pojo.User">
select * from t_user where id = #{id}
</select>
</mapper>
package com.bobo.test;
import com.bobo.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.jupiter.api.Test;
import java.io.InputStream;
import java.util.List;
public class TestDemo01 {
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
// 5.MyBatis中默认是不会自动提交DML操作的数据的
sqlSession.commit();
// 6.关闭会话
sqlSession.commit();
}
@Test
public void test02() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
User user = new User(19,"翠花","北京海淀","女");
int i = sqlSession.update("com.bobo.pojo.User.updateUser", user);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int i = sqlSession.delete("com.bobo.pojo.User.deleteUser", 20);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void queryAll() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List<User> lists = sqlSession.selectList("com.bobo.pojo.User.queryAll");
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void queryById() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List<User> lists = sqlSession.selectList("com.bobo.pojo.User.queryById",19);
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
}