Mybaits3
根据B站大佬狂神说的视频学习的Mybatis,然后整理的笔记。
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'狂神','123456'),(2,'张三','123456'),(3,'李四','123890')
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.29version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
Driver=com.mysql.jdbc.Driver
Url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&userUnicode=true&characterEncoding=utf-8
UserName=root
PassWord=123456
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">properties>
<typeAliases>
<typeAlias type="com.liushq.pojo.User" alias="user" />
<package name="com.liushq.pojo" />
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">transactionManager>
<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="com/liushq/mapper/UserMapper.xml" />
mappers>
configuration>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.liushqgroupId>
<artifactId>mybatis-studyartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.29version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
project>
package com.liushq.utils;
public class MyBaitsUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
package com.liushq.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package com.liushq.mapper;
public interface UserMapper {
List<User> getUserList();
List<User> getUserListByMap(Map<String,Object> map);
User getUserInfo(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liushq.mapper.UserMapper">
<select id="getUserList" resultType="com.liushq.pojo.User">
select * from user;
select>
<select id="getUserListByMap" parameterType="map" resultType="com.liushq.pojo.User">
select * from mybatis.user where name like '%${name}%';
select>
<select id="getUserInfo" parameterType="int" resultType="com.liushq.pojo.User">
select * from mybatis.user where id=#{id};
select>
<insert id="addUser" parameterType="com.liushq.pojo.User">
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd})
insert>
<update id="updateUser" parameterType="com.liushq.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
delete>
mapper>
package com.liushq.service;
public interface UserService {
List<User> getUserList();
List<User> getUserListByMap(String name);
User getUserInfo(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
package com.liushq.service.Impl;
public class UserServiceImpl implements UserService {
private SqlSession sqlSession = MyBaitsUtils.getSqlSession();
private UserMapper mapper = sqlSession.getMapper(UserMapper.class);
@Override
public List<User> getUserList() {
List<User> list=mapper.getUserList();
sqlSession.close();
return list;
}
@Override
public List<User> getUserListByMap(String name) {
Map<String,Object> map=new HashMap<>();
map.put("name",name);
List<User> list=mapper.getUserListByMap(map);
sqlSession.close();
return list;
}
@Override
public User getUserInfo(int id) {
User user=mapper.getUserInfo(id);
sqlSession.close();
return user;
}
@Override
public int addUser(User user) {
int res=mapper.addUser(user);
sqlSession.commit();
sqlSession.close();
return res;
}
@Override
public int updateUser(User user) {
int res=mapper.updateUser(user);
sqlSession.commit();
sqlSession.close();
return res;
}
@Override
public int deleteUser(int id) {
int res=mapper.deleteUser(id);
sqlSession.commit();
sqlSession.close();
return res;
}
}
package com.liushq.service;
import com.alibaba.fastjson.JSONObject;
public class UserServiceTest {
private UserService service=new UserServiceImpl();
@Test
public void getUserList(){
List<User> userList = service.getUserList();
for (User user : userList) {
System.out.println(JSONObject.toJSONString(user));
}
}
@Test
public void getUserListByMap(){
List<User> userList = service.getUserListByMap("i");
for (User user : userList) {
System.out.println(user);
}
}
@Test
public void getUserInfo(){
User user = service.getUserInfo(1);
System.out.println(user);
}
@Test
public void addUser(){
User user=new User(5,"xiaoma1","123123");
int res= service.addUser(user);
System.out.println(res>0?"添加成功":"添加失败");
}
@Test
public void updateUser(){
User user=new User(5,"xiaoma1111","123123111");
int res= service.updateUser(user);
System.out.println(res>0?"修改成功":"修改失败");
}
@Test
public void deleteUser(){
int res= service.deleteUser(4);
System.out.println(res>0?"删除成功":"删除失败");
}
}
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.83version>
<scope>testscope>
dependency>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liushq.mapper.UserMapper">
mapper>
<select id="getUserInfo" parameterType="int" resultType="com.liushq.pojo.User">
select * from mybatis.user where id=#{id};
select>
<insert id="addUser" parameterType="com.liushq.pojo.User">
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd})
insert>
<update id="updateUser" parameterType="com.liushq.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
delete>
注意点:增、删、改操作需要提交事务!
mybatis中可以配置的内容如下:
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。例如:
<properties resource="db.properties">properties>
<properties resource="db.properties">
<property name="UserName" value="root"/>
<property name="PassWord" value="123456"/>
properties>
如果一个属性在不只一个地方进行了配置,比如:UserName跟PassWord,在db.properties配置文件里有,在properties标签中也有,那么,MyBatis 将按照下面的顺序来加载:
设置好的属性后,就可以在整个配置文件中用来替换需要动态配置的属性值。例如:
<dataSource type="POOLED">
<property name="driver" value="${Driver}"/>
<property name="url" value="${Url}" />
<property name="username" value="${UserName}" />
<property name="password" value="${PassWord}" />
dataSource>
这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。 下表描述了设置中各项设置的含义、默认值等。这里只罗列一些常用的、比较重要的,其他的可以自行看官网文档。
设置名 | 描述 | 有效值 | 默认值 |
---|---|---|---|
cacheEnabled | 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。 | true或false | true |
lazyLoadingEnabled | 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 | true或false | false |
mapUnderscoreToCamelCase | 是否开启驼峰命名自动映射,即从经典数据库列名 A_COLUMN 映射到经典 Java 属性名 aColumn。 | true或false | false |
logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J、LOG4J(3.5.9 起废弃)、LOG4J2、 JDK_LOGGING、COMMONS_LOGGING、STDOUT_LOGGING、NO_LOGGING | 未设置 |
一个配置完整的 settings 元素的示例如下:
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="safeResultHandlerEnabled" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
<setting name="defaultScriptingLanguage" value="org.apache.ibatis.scripting.xmltags.XMLLanguageDriver"/>
<setting name="defaultEnumTypeHandler" value="org.apache.ibatis.type.EnumTypeHandler"/>
<setting name="callSettersOnNulls" value="false"/>
<setting name="returnInstanceForEmptyRow" value="false"/>
<setting name="logPrefix" value="exampleLogPreFix_"/>
<setting name="logImpl" value="SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING"/>
<setting name="proxyFactory" value="CGLIB | JAVASSIST"/>
<setting name="vfsImpl" value="org.mybatis.example.YourselfVfsImpl"/>
<setting name="useActualParamName" value="true"/>
<setting name="configurationFactory" value="org.mybatis.example.ConfigurationFactory"/>
settings>
类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。例如:
<typeAliases>
<typeAlias type="com.liushq.pojo.User" alias="User" />
typeAliases>
<typeAliases>
<package name=""com.liushq.pojo"/>
typeAliases>
在UserMapper.xml中,没设置别名前
<select id="getUserList" resultType="com.liushq.pojo.User">
select * from user;
select>
设置别名后,resultType中的类名可以直接使用别名
<select id="getUserList" resultType="User">
select * from user;
select>
官网中有详细的别名介绍,可以自行查看。
MyBatis 可以配置成适应多种环境,这种机制有助于将 SQL 映射应用于多种数据库之中,尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">transactionManager>
<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>
<environment id="test">
...
environment>
environments>
既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要来定义 SQL 映射语句了。 但首先,我们需要告诉 MyBatis 到哪里去找到这些语句。 在自动查找资源方面,Java 并没有提供一个很好的解决方案,所以最好的办法是直接告诉 MyBatis 到哪里去找映射文件。
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper class="com.liushq.mapper.UserMapper"/>
<package name="com.liushq.mapper"/>
mappers>
更新中。。。