mybatis基础
入门_MyBatis中文网
maven安装
mysql
1 2 3 4 5 6
| <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
association(对象)
- property 对象名
- column 数据库名 向子查询传参用这个 column=”id = user_id”
- javaType 类名
- 嵌套增删改查 例:select
例:association(子查询) column传参
例:association-2
collection
ofType
例:collection
例:collection-2
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配置文件
- settings
- 配置_MyBatis中文网
- mappers
- mapper
- resource 指定xml
- class 指定class,接口必须和xml同名
- 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>
<properties resource="db.properties"/>
<typeAliases> <typeAlias type="cn.sdadgz.pojo.User" alias="User"/> <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(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList();
for (User user : userList){ System.out.println(user); }
sqlSession.close(); } }
|
增删改必须提交事务
万能map
注解
动态sql
where(set同理)通过trim实现
sql片段
choose(同switch)
- when(同case)
- otherwise(同default)
foreach