MyBatis的xml设置(焦点设置)
configuration(设置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处置器)
objectFactory(工具工厂)
plugins(插件)
environments(环境设置)
environment(环境变量)
transactionManager(事务治理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
MyBatis的Mapper映射
Mapper映射文件只有几个顶级元素(按照应被界说的顺序如下): cache – 对给定命名空间的缓存设置。 cache-ref – 对其他命名空间缓存设置的引用。 resultMap – 是最庞大也是最壮大的元素,用来形貌若何从数据库效果集中来加载工具。 sql – 可被其他语句引用的可重用语句块。 insert – 映射插入语句 update – 映射更新语句 delete – 映射删除语句 select – 映射查询语句
MyBatis也可以使用注解开发
天生代码时刻,设置: <javaClientGenerator type="ANNOTATEDMAPPER"... 天生注解代码,不天生xml代码了
MyBatis开发使用注解照样xml?
方式 | 优点 | 瑕玷 |
Xml | Xml与接口星散,利便治理,庞大的SQL不影响代码的可读性 | 过多的Xml设置文件 |
Annotation | 接口就能看到SQL语句,可读性强,不需要去找xml文件,利便 | 代码和SQL混杂,庞大一点过于杂乱 |
MyBatis批量插入
1 通俗for循环(此种方式对于数据库的I/O过于频仍,不适合大数据量的操作)
for (int i = 0; i < 500; i++) { user = new User(); user.setId("id" + i); user.setName("name" + i); userMapper.insert(user); }
2 ExeutorType.BATCH(设置批量模式),把SQL语句发个数据库,数据库预编译好,然后数据库守候需要运行的参数,吸收到参数一次性运行
session = sqlSessionFactory.openSession(ExecutorType.BATCH, true); for (int i=0; i<500; i++) { UUserInfo userInfo = new UUserInfo(); userInfo.setPhone("1346262122" + i); userInfo.setUserName("张" + i); uUserInfoMapper.insert(userInfo); }
3 传入一个数组或聚集,标签<foreach>插入
<foreach item="item" collection="strs.split(',')" separator="," open="(" close=")"> #{item} </foreach>
方式 | 性能 | 说明 |
For循环 | 性能低,IO高 | |
ExeutorType.BATCH | 性能居中 | |
<foreach>标签拼SQL | 性能最高 |
有SQL长度限制,mysql默认吸收SQl的长度为10486576(1M),该方式若跨越1M 会抛出异常 |
MyBatis团结查询
一对一关系
在一对一关系中,A表中的一行最多只能匹配B表的一行,反之亦然.这种关系并不常见,由于一样平常来说,这种关系的信息会保留在一张表中,固然也可以行使一对一关系来保留,好比支解具有多列的表.(如:一个人只有一个身份证)
<association>标签
<resultMap id="OneToOneBaseResultMap" type="com.test.mybatis.model.Person"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="nick" jdbcType="VARCHAR" property="nick" /> <result column="phone" jdbcType="VARCHAR" property="phone" /> <result column="sex" jdbcType="INTEGER" property="sex" /> <association property="idCard" javaType="com.test.mybatis.model.IdCard"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="personId" jdbcType="INTEGER" property="personid" /> <result column="realName" jdbcType="VARCHAR" property="realname" /> <result column="idCard" jdbcType="VARCHAR" property="idcard" /> </association> </resultMap>
一对多关系
动手实现一个简单的 rpc 框架到入门 grpc (上)
一对多是最通俗的一种关系,在这种关系中,A表的一行可以匹配B表中的多行,然则B表中的一行只能匹配A表中的一行.举例:(一个部门可以有多个员工)
<collection>标签
<resultMap type="com.mybatis.bean.Department" id="MyDept"> <id column="did" property="id"/> <result column="dept_name" property="departmentName"/> <!-- collection界说关联聚集类型的属性的封装规则 ofType:指定聚集内里元素的类型 --> <collection property="emps" ofType="com.mybatis.bean.Employee"> <!-- 界说这个聚集中元素的封装规则 --> <id column="eid" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> </collection> </resultMap>
多对多关系
在多对多的关系中,A表的一行可以匹配B表的多行.反之亦然.要建立这种关系,需要界说第三张表,也可以称之为连系表或者关系表.它的主键是由A表和B表的外部键组成. 举例(一个用户有多个角色,一个角色可以对应多个用户)
<collection>标签
MyBatis插入并返回主键
方式一:(数据库要设置主键自增进)
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="com.test.mybatis.model.UUserInfo">
useGeneratedKeys="true"示意使用主键自增 keyProperty="id"示意将主键自增后的主键值赋值给实体类中的id属性
parameterType="com.test.mybatis.model.UUserInfo" 实体类,自增后的主键值将会赋值给该实体类的id属性
方式二:(一样平常用在<insert>标签内里)
<selectKey keyProperty="id" resultType="integer" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey>
SELECT LAST_INSERT_ID()示意查询出刚刚插入的的纪录自增进id order='AFTER' 示意先执行插入语句,之后再执行查询语句
MyBatis的SQL注入攻击
注入攻击的危害
1:数据库被拖库(把数据从数据库拉取出来,造成数据泄露)
2.主要信息被泄露
注入的本质是把用户输入的数据当做有用代码执行. Mybatis的预编译机制可以有用防止SQL注入攻击. '#{}' 和 '${}'的区别: '#{}':MyBaits会首先对其举行预编译,将#{user_ids}替换成?占位符,然后在执行时替换成现实传入的user_id值,**并在双方加上单引号,以字符串方式处置。 '${}':简朴的字符串拼接
栗子:
uUserInfoMapper.selectByIn("select user_name from u_user_info"); where user_name in (${userName}) 可使用MyBatis自带循环标签解决SQL语句动态拼接的问题: select * from news where id in <foreach collection="ids" item="item" open="("separator="," close=")"> #{item} </foreach>
MyBatis的自界说类型转换器(实现数据类型和数据库数据类型的映射关系 如:String和VARCHAR的对应关系) 需求:实现对数据库身份证存储的加密
1、界说自己的HandlerType实现TypeHandler接口或者继续BaseTypeHandler类 2、笼罩其四个方式; 3、在焦点设置文件mybatis-config.xml中设置<typeHandlers>标签或者在映射文件的增、改、查位置单独设置; <typeHandlers> <typeHandler javaType="com.test.mybatis.typehandler.UserIdCard" handler="com.bjpowernode.mybatis.typehandler.CryptTypeHandler"/> </typeHandlers> <result column="idCard" jdbcType="VARCHAR" property="userIdCard" javaType="com.bjpowernode.mybatis.typehandler.UserIdCard"
typeHandler="com.test.mybatis.typehandler.CryptTypeHandler"/> <if test="userIdCard != null"> #{userIdCard, jdbcType=VARCHAR, typeHandler=com.test.mybatis.typehandler.CryptTypeHandler}, </if>
系统自带的类型转换器StringTypeHandler
public class StringTypeHandler extends BaseTypeHandler<String> { @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getString(columnName); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getString(columnIndex); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getString(columnIndex); } }
MyBatis的动态SQL
if –判断标签
choose (when, otherwise) –多项选择 ,与页面的 jstl 标签异常类似
trim (where, set) –修剪、格式化,添加前后缀的一个标签
foreach –循环
<select id="dynamicChoose" parameterType="News" resultType="News"> select * from news where 1 = 1 <choose> <when test="title != null"> and title = #{title} </when> <when test="content != null"> and content = #{content} </when> <otherwise> and owner = "zhangsan" </otherwise> </choose> </select>
MyBatis的SQL片断使用
<sql id="Base_Column_List"> id, name </sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from user where id = #{id,jdbcType=INTEGER} </select>
MyBatis的一级.二级缓存
一级缓存是sqlSession级别,默认开启
二级缓存是mapper级别,默认关闭,缓存命中率低
MyBatis的事务治理:
<transactionManager type="JDBC"> <property name="..." value="..."/> </transactionManager>
若是你正在使用 Spring + MyBatis,则没有需要设置事务治理器, 由于 Spring 模块会使用自带的治理器来笼罩前面的设置.
原创文章,作者:admin,如若转载,请注明出处:https://www.2lxm.com/archives/21241.html