POJO、Javabean
首先我们来看一下pojo和javabean
pojo是一种java的普通对象,没有任何类继承、没有任何接口实现、没有任何框架接入的对象
javanbean可以看做是一种特殊的pojo对象,它是由java写成的可重用组件,是一种规范的写法,而不是一种技术,以下是它的规范性:
1.所有属性为private。
2.这个类必须有一个公共的缺省构造函数。即是提供无参数的构造器。
3.这个类的属性使用getter和setter来访问,其他方法遵从标准命名规范。
4.这个类应是可序列化的。实现serializable接口。
区别:POJO主要用于数据的临时传递,它只能装载数据,而不具有业务逻辑处理的能力。
Javabean虽然数据的获取与POJO一样,但是javabean当中可以有其它的方法。
是支持定制化sql、存储过程以及高级映射的持久化框架
简化了数据库连接,精力放在关注sql语句
1.mybatis开发dao两种方法:
servlet(dao接口 dao实现)
mybatis mapper接口(dao接口)代理开发
2.mybatis配置文件 sqlMapConfig.xml
3.核心:输入映射、输出映射
4.mybatis动态sql
JDBC问题总结:
1.数据库连接:连接数据库过于频繁,资源浪费,影响数据库性能
2.sql语句:sql需要修改时,需要重新编译java代码,硬编码,不利于系统维护(mybatis:把sql配置到xml中,java代码无需重新编译)
3.preparestatement设置参数符号位置 硬编码(mybatis:把参数配置到配置文件)
4.结果集(resultset)遍历数据时对表的字段 硬编码(mybatis:查询的结果集映射成java对象)
mybatis
SqlMapConfig(配置文件、配置sql语句、mapper、mapper2…)
<?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>
<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="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
SqlSessionFactory(SqlSessionFactoryBuilder创建):创建SqlSession
SqlSesiion:操作数据库
Excutor(执行器):SqlSession内执行器操作数据库
mapped statement(底层封装对象):Java对象输入输出结果集
**占位符:#{} ‘1’=‘1’,“wang”=“wang”
字符串拼接
:
1
=
1
,
w
a
n
g
=
w
a
n
g
(
模
糊
查
询
使
用
′
{}:1=1,wang=wang (模糊查询使用 '%
:1=1,wang=wang(模糊查询使用′{_parameter}%’) **
#方式能够很大程度防止sql注入。
$方式无法防止Sql注入。
映射器类可以使用两种方法:
1.Java注解:较为复杂的语句力不从心、不方便阅读、尽量使用XML
public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}
2.XML配置
<mapper namespace="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
Mybatis中Mapper动态代理的实现原理
定义一个接口 方法名,参数需要与XML定义保持一致。
namespace必须对应着map接口的全类名
思想:还不够深入,继续学习
!!!学会了:Junit单元测试:添加注解@Test 测试单个函数、类
SqlMapConfig主配置文件
(1)properties、property属性
****可以通过三种方式设置property
1.通过<properties元素里面配置<property元素
2.单独编写property文件,通过properties导入
<properties resource="mysql.properties">
</properties>
3.人工编写property类
优先级次序如下:第三种方式>第二种方式>第一种方式。即如果三种方式都配置了同一个配置项,那么优先级高的配置方式的配置值生效。
(2)typeAliases配置别名
我们在使用 com.demo.entity. UserEntity 的时候,我们可以直接配置一个别名user, 这样以后在配置文件中要使用到com.demo.entity. UserEntity的时候,直接使用User即可
以包名配置(多个bean):
<typeAliases>
<package name="com.bean"/>
</typeAliases>
(3)mappers(映射器)
配置多个mapper:尽量使用package name:mapper包
当mapper过多时,一个一个添加mapper太过麻烦
告诉 MyBatis 到哪里去找到这些sql映射语句
-
使用相对于类路径的资源引用 -
<mapper resource=“org/mybatis/builder/AuthorMapper.xml”/ -
使用完全限定资源定位符(URL)
<mapper url=“file:///var/mappers/AuthorMapper.xml”/ -
使用映射器接口实现类的完全限定类名
<mapper class=“org.mybatis.builder.AuthorMapper”/ -
将包内的映射器接口实现全部注册为映射器
<package name=“org.mybatis.builder”/
(5)paparmeterType输入映射
1.基本数据类型 int、String等:username
2.pojo(plain old java object)对象:当输入映射为pojo对象时,对应的占位符应该是对象的属性值username
3.pojo封装对象:即一个对象里包含另一个对象,对应的的占位符为包装对象的属性值user.username
(6)resultType、resultMap输出映射
resultType:自动映射,一般用于数据库和bean对象名称一致时,无需建立数据库和bean对象属性的映射
resultMap:手动映射,一般用于数据库和bean对象属性名不完全匹配
<select id="getAllCountry" resultMap="Map">
select * from country
</select>
<resultMap id="Map" type="Country">
把bean对象的属性id和数据库键名称c_id映射
<result property="id" column="c_id"/>
</resultMap>
关联查询
(1)一对一关联查询:查询一个对象时,把与该对象关联的一个对象查询出来
(2)一对多关联查询:查询一个对象时,同时把与该对象关联的其他多个对象查询出来
一对一关联查询:查询用户表对应的与国家关联的所有信息 javaType普通类对象
需要使用resultMap里的association id一般指定主键,result指定其他任意键(提高性能)
<select id="assioationSelect" resultMap="Uservo2">
select user.u_id,
user.u_username,
user.u_sex,
country.c_id,
country.c_countryname
from user
left join
country on
user.u_id=country.c_id;
</select>
<resultMap id="Uservo2" type="UserVo2">
<id property="u_id" column="u_id"/>
<result property="u_sex" column="u_sex"/>
<result property="u_username" column="u_username"/>
<association property="country" javaType="Country">
<id property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap>
这里使用一个User的继承包装类,包装了国家类,包含国家对象和用户类的属性,重写了toString()方法,查询u_id等于c_id的记录
一对多关联查询:查询国家表里与用户关联的所有信息
需要使用resultMap里的collection 其他与一对一关联查询一致 ofType:集合属性的泛型类型
<select id="connectionSelect" resultMap="CountryVo">
select country.c_id,
country.c_countryname,
user.u_id,
user.u_username,
user.u_sex
from country
left join user on
user.u_id=country.c_id;
</select>
<resultMap id="CountryVo" type="CountryVo">
<id property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
<collection property="userList" ofType="User">
<id property="u_id" column="u_id"/>
<result property="u_username" column="u_username"/>
<result property="u_sex" column="u_sex"/>
</collection>
</resultMap>
动态sql语句
多条件查询
select * from user
where u_sex=#{u_sex}
and u_username like "%${u_username}%"
and u_cid=#{u_cid}
if
<if test="title != null">
AND title like #{title}
</if>
where:解决and符号存在where之后造成sql语法错误
<where>
<if test="u_sex!=null and u_sex!=''">
u_sex=#{u_sex}
</if>
<if test="u_username!=null and u_username!=''">
and u_username like "%"#{u_username}"%"
</if>
<if test="u_cid!=null">
and u_cid=#{u_cid}
</if>
</where>
trim
prefix suffix prefixOverrides suffixOverrides 分别表示where的前后位置和and、or的前后位置
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
set:用于动态包含需要更新的列,而舍去其它的。解决字段后逗号问题
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
foreach:动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。 可以是数组或者集合 collection分别为array、list
<select id="getByIds" resultType="User">
select * from user where u_id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
sql语句:缩减mapper语句 ,可以把重复的语句写在sql标签中,再在select等语句中通过include标签加入
bind:使用${}拼接字符串无法防止sql注入,bind标签可以解决这个问题
<bind name="username" value="'%'+u_username+'%'"/>
select * from user u_username like #{username}