【Mybatis】SqlMapConfig.xml核心配置文件

1、properties 属性

SqlMapConfig.xml可以引用java属性文件中的配置信息如下:

在classpath下定义db.properties文件(key-value形式注意不要出现空格)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

2、SqlMapConfig.xm在核心配置文件中引用

//引用文件
	
		
			
			
				//使用el表达式
				
				
				
			
		
	
注意: MyBatis 将按照下面的顺序来加载属性:
在 properties 元素体内定义的属性首先被读取。 
然后会读取properties 元素中resource或 url 加载的属性,它会覆盖已读取的同名属性。 

3 typeAliases(类型别名)

3.1Mybatis支持的别名

别名

映射的类型

_byte

byte

_long

long

_short

short

_int

int

_integer

int

_double

double

_float

float

_boolean

boolean

string

String

byte

Byte

long

Long

short

Short

int

Integer

integer

Integer

double

Double

float

Float

boolean

Boolean

date

Date

decimal

BigDecimal

bigdecimal

BigDecimal

map

Map

3.2 Mybatis自定义别名的方式

在SqlMapConfig.xml中配置:


	
	
	
	
	

4、mappers映射器

Mapper配置的几种方法:

4.1

使用相对于类路径的资源
如:

4.2

使用mapper接口类路径
如:
注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。

4.3

注册指定包下的所有mapper接口
如:

注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。

5、所有的配置写法

SqlMapConfig.xml中




	
	
	 
		

		
		
		
	

	
	
		
		
		
		
		
			
			
			
			
		
		
	
	
	
		
		
		

		
		
		
	

未使用mapper接口的方式下的User.xml






	
	
	
	
	
	
	
	
		
		
			select LAST_INSERT_ID()
		
		insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
	
	
	
		delete from user where id=#{id}
	
	
	
		update user set username=#{username} where id=#{id}
	

使用Mapper动态接口的UserMapper.xml







	
	
	
	
	
	
	
		
		
			select LAST_INSERT_ID()
		
		insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
	

你可能感兴趣的:(SSM框架)