mybatis基础

入门_MyBatis中文网

maven安装

mysql

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>

mybatis

1
2
3
4
5
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>

配置文件

java

[类名]Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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="cn.sdadgz.dao.UserMapper">
<!--显示全部-->
<select id="getUserList" resultType="cn.sdadgz.pojo.User">
select * from mybatis.user
</select>
<!--查询指定-->
<select id="getUserById" parameterType="int" resultType="cn.sdadgz.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<!--插入-->
<insert id="addUser" parameterType="cn.sdadgz.pojo.User">
insert into mybatis.user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<!--更新-->
<update id="updateUser" parameterType="cn.sdadgz.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id = #{id}
</update>
<!--删除-->
<delete id="deleteUser" parameter="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
  • id 调用时名字
  • resultType 返回值类型
  • parameterType 参数类型
resultMap
result

1657182916658

association(对象)
  1. property 对象名
  2. column 数据库名 向子查询传参用这个 column=”id = user_id”
  3. javaType 类名
  4. 嵌套增删改查 例:select

例:association(子查询) column传参

1657294027376

例:association-2

1657294812642

collection

ofType

例:collection

1657297461218

例:collection-2

1657298641599

utils工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.sdadgz.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 MybatisUtils {

private static SqlSessionFactory sqlSessionFactory;

static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}

public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}

resources

mybatis-config.xml配置文件

  1. settings
    1. 配置_MyBatis中文网
  2. mappers
    1. mapper
      1. resource 指定xml
      2. class 指定class,接口必须和xml同名
      3. package 自动扫描,接口必须和xml同名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>

<!--里面可以放property定义值-->
<properties resource="db.properties"/>

<!--起别名-->
<typeAliases>
<!--指定类起别名-->
<typeAlias type="cn.sdadgz.pojo.User" alias="User"/>
<!--自动扫描默认首字母小写,再类前加注解@Alias("[别名]")自定义-->
<package name="cn.sdadgz.pojo"/>
</typeAliases>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="cn/sdadgz/dao/UserMapper.xml"/>
</mappers>
</configuration>

db.properties

1
2
3
4
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://49.232.139.28:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=[密码]

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cn.sdadgz.dao.UserDao;
import cn.sdadgz.pojo.User;
import cn.sdadgz.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;


public class UserDaoTest {

@Test
public void test(){
// 调用工具类
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 接口mapper
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// 方法
List<User> userList = userMapper.getUserList();

for (User user : userList){
System.out.println(user);
}

sqlSession.close();
}
}

增删改必须提交事务

1
2
// 增删改后面加这个
sqlSession.commit();

万能map

1657178220228

注解

1657280158391

动态sql

where(set同理)通过trim实现

1657345036687

sql片段

1657349838943

choose(同switch)

  1. when(同case)
  2. otherwise(同default)

foreach

1657375512013

1657374707985