mybatis-210717-02—全局配置文件
properties_引入外部配置文件
mybatis中xml中properties配置:
https://mybatis.org/mybatis-3/zh/configuration.html#properties
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>
< properties resource = " dbconfig.properties" > </ properties>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
</ mappers>
</ configuration>
dbconfig.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_test_210716_01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
jdbc.username=root
jdbc.password=admin
settings_运行时行为设置
mybatis中xml中settings配置:
https://mybatis.org/mybatis-3/zh/configuration.html#settings
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>
< properties resource = " dbconfig.properties" > </ properties>
< settings>
< setting name = " mapUnderscoreToCamelCase" value = " true" > </ setting>
</ settings>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
</ mappers>
</ configuration>
EmployeeMapper.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" >
< select id = " getEmpById" resultType = " com.bgy.bean.Employee" >
select
id,last_name,gender,email
from
tbl_employee
where
id = #{id}
</ select>
</ mapper>
typeAliases_别名
mybatis中xml中typeAliases配置:
https://mybatis.org/mybatis-3/zh/configuration.html#typeAliases
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>
< properties resource = " dbconfig.properties" > </ properties>
< settings>
< setting name = " mapUnderscoreToCamelCase" value = " true" > </ setting>
</ settings>
< typeAliases>
< typeAlias type = " com.bgy.bean.Employee" alias = " employee" />
< package name = " com.bgy.bean" />
</ typeAliases>
< environments default = " development" >
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
</ mappers>
</ configuration>
Employee.java
package com. bgy. bean ;
import org. apache. ibatis. type. Alias ;
@Alias ( value = "emp" )
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getLastName ( ) {
return lastName;
}
public void setLastName ( String lastName) {
this . lastName = lastName;
}
public String getEmail ( ) {
return email;
}
public void setEmail ( String email) {
this . email = email;
}
public String getGender ( ) {
return gender;
}
public void setGender ( String gender) {
this . gender = gender;
}
@Override
public String toString ( ) {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]" ;
}
}
EmployeeMapper.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.bgy.dao.EmployeeMapper" >
< select id = " getEmpById" resultType = " emp" >
select
id,last_name,gender,email
from
tbl_employee
where
id = #{id}
</ select>
</ mapper>
typeHandlers_类型处理器
mybatis中xml中typeHandlers配置:
https://mybatis.org/mybatis-3/zh/configuration.html#typeHandlers
environment_运行环境
mybatis中xml中environmen配置:
https://mybatis.org/mybatis-3/zh/configuration.html#environments
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>
< properties resource = " dbconfig.properties" > </ properties>
< settings>
< setting name = " mapUnderscoreToCamelCase" value = " true" > </ setting>
</ settings>
< typeAliases>
< typeAlias type = " com.bgy.bean.Employee" alias = " employee" />
< package name = " com.bgy.bean" />
</ typeAliases>
< environments default = " development" >
< environment id = " test" >
< transactionManager type = " " > </ transactionManager>
< dataSource type = " " > </ dataSource>
</ environment>
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
</ mappers>
</ configuration>
databaseIdProvider_多数据库支持
mybatis中xml中databaseIdProvider配置:
https://mybatis.org/mybatis-3/zh/configuration.html#databaseIdProvider
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>
< properties resource = " dbconfig.properties" > </ properties>
< settings>
< setting name = " mapUnderscoreToCamelCase" value = " true" > </ setting>
</ settings>
< typeAliases>
< typeAlias type = " com.bgy.bean.Employee" alias = " employee" />
< package name = " com.bgy.bean" />
</ typeAliases>
< environments default = " development" >
< environment id = " test" >
< transactionManager type = " " > </ transactionManager>
< dataSource type = " " > </ dataSource>
</ environment>
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< databaseIdProvider type = " DB_VENDOR" >
< property name = " MySQL" value = " mysql" />
< property name = " Oracle" value = " oracle" />
< property name = " SQL Server" value = " sqlserver" />
</ databaseIdProvider>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
</ mappers>
</ configuration>
EmployeeMapper.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.bgy.dao.EmployeeMapper" >
< select id = " getEmpById" resultType = " emp" databaseId = " mysql" >
select
id,last_name,gender,email
from
tbl_employee
where
id = #{id}
</ select>
< select id = " getEmpById" resultType = " emp" databaseId = " oracle" >
select
id,last_name,gender,email
from
tbl_employee
where
id = #{id}
</ select>
</ mapper>
mappers_sql映射路径
mybatis中xml中mappers配置:
https://mybatis.org/mybatis-3/zh/configuration.html#mappers
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>
< properties resource = " dbconfig.properties" > </ properties>
< settings>
< setting name = " mapUnderscoreToCamelCase" value = " true" > </ setting>
</ settings>
< typeAliases>
< typeAlias type = " com.bgy.bean.Employee" alias = " employee" />
< package name = " com.bgy.bean" />
</ typeAliases>
< environments default = " development" >
< environment id = " test" >
< transactionManager type = " " > </ transactionManager>
< dataSource type = " " > </ dataSource>
</ environment>
< environment id = " development" >
< transactionManager type = " JDBC" />
< dataSource type = " POOLED" >
< property name = " driver" value = " ${jdbc.driver}" />
< property name = " url" value = " ${jdbc.url}" />
< property name = " username" value = " ${jdbc.username}" />
< property name = " password" value = " ${jdbc.password}" />
</ dataSource>
</ environment>
</ environments>
< databaseIdProvider type = " DB_VENDOR" >
< property name = " MySQL" value = " mysql" />
< property name = " Oracle" value = " oracle" />
< property name = " SQL Server" value = " sqlserver" />
</ databaseIdProvider>
< mappers>
< mapper resource = " EmployeeMapper.xml" />
< mapper class = " com.bgy.dao.EmployeeMapperAnnotation" />
</ mappers>
</ configuration>
EmployeeMapperAnnotation.java
package com. bgy. dao ;
import org. apache. ibatis. annotations. Select ;
import com. bgy. bean. Employee ;
public interface EmployeeMapperAnnotation {
@Select ( "select * from tbl_employee where id=#{id}" )
public Employee getEmpById ( Integer id) ;
}
MyBatisTest.java
package com. bgy. bean. test ;
import java. io. IOException ;
import java. io. InputStream ;
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 com. bgy. bean. Employee ;
import com. bgy. dao. EmployeeMapper ;
import com. bgy. dao. EmployeeMapperAnnotation ;
class MyBatisTest {
public SqlSessionFactory getSqlSessionFactory ( ) throws IOException {
String resource = "mybatis-config.xml" ;
InputStream inputStream = Resources . getResourceAsStream ( resource) ;
return new SqlSessionFactoryBuilder ( ) . build ( inputStream) ;
}
@Test
public void test01 ( ) throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ( ) ;
SqlSession openSession = sqlSessionFactory. openSession ( ) ;
try {
EmployeeMapper mapper = openSession. getMapper ( EmployeeMapper . class ) ;
Employee employee = mapper. getEmpById ( 1 ) ;
System . out. println ( "接口式编程" ) ;
System . out. println ( mapper. getClass ( ) ) ;
System . out. println ( employee) ;
} finally {
openSession. close ( ) ;
}
}
@Test
public void test02 ( ) throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ( ) ;
SqlSession openSession = sqlSessionFactory. openSession ( ) ;
try {
EmployeeMapperAnnotation mapper = openSession. getMapper ( EmployeeMapperAnnotation . class ) ;
Employee employee = mapper. getEmpById ( 1 ) ;
System . out. println ( employee) ;
} finally {
openSession. close ( ) ;
}
}
}