一、开发方式
MyBatis-Dao层Mapper接口化开发
二、注意事项
1、Mapper接口与Mapper.xml映射文件要满足4个对应
(1)Mapper接口的全类名必须与Mapper映射文件中的namespace相同
(2)Mapper接口中的每一个方法名在Mapper映射文件中都要有一个id为方法名的标签相对应
(3)Mapper映射文件中的标签的参数类型必须与Mapper接口中的对应方法的参数类型相同
(4)Mapper映射文件中的标签的结果类型必须与Mapper接口中的对应方法的结果类型相同
三、参数传递
1、单参数传递(基本类型参数)
直接使用方法的参数名传递参数
Admin findAdminById(int id);
<select id="findAdminById" parameterType="int" resultType="Admin">
select * from admin where id = #{id}
</select>
2、少参数传递(参数是引用类型时必须使用@Param注解标签对其进行标记)
使用注解标签传递参数
Admin login(@Param("acc") String account, @Param("pwd") String password);
<select id="login" resultType="Admin">
select * from admin where account = #{acc} and password = #{pwd}
</select>
3、多参数传递(对象类型参数)
使用对象传递参数
void saveAdmin(Admin admin);
<insert id="saveAdmin" parameterType="admin">
insert into admin(account,password,gender)value(#{account},#{password},#{gender})
</insert>
注意:执行增删改sql语句时必须要提交数据库事务(一次与数据库交互的过程管理)
两种提交数据库事务的方式
方式一
给定参数设置为自动提交数据库事务
sqlSessionFactory.openSession(true);
方式二
调用方法手动提交数据库事务
sqlSession.commit();
对比两种方式,最好使用第二种,可以使得多条sql在遇到异常时保持状态一致,要么都提交成功,要么都提交失败,避免出现交钱不下单问题
四、#{}表达式和${}表达式
1、#{}表达式
(1)作用:传递参数值
(2)传递方式:预编译方式传递
(3)优点:安全,避免出现当字符串'or 1=1'作为表删除条件表内数据全部删除的情况
(4)用途:用于向sql中传值
2、${}表达式
(1)作用:传递列名
(2)传递方式:字符串拼接方式传递
(3)优点:
通过加‘’可实现参数值传递
(4)缺点:
当字符串'or 1=1'作为表删除条件时表内数据将会被全部删除
(5)用途:
用于向sql中动态传列名(如排序列名传递和数据显示列名传递两个实际应用场景)
(6)使用场景举例-对查询出来的管理员信息按照可选择方式进行排序(排序列名传递)
- 核心代码:
Java接口文件中
List<Admin> findAdmins(@Param("col") String col);// 查询所有数据并将查询结果按指定方式排序后输出
XML配置文件中
<select id="findAdmins" resultType="com.ffyc.mybatispro.model.Admin" parameterType="string">
select * from admin order by ${col}
</select>
- 测试代码:
A.使用查询结果中id列的值对查询结果进行排序
SqlSession sqlSession = MybatisUtil.getSqlSession();
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
List<Admin>admins=adminDao.findAdmins("id");
System.out.println(admins);
sqlSession.close();

B.使用查询结果中account列的值对查询结果进行排序
SqlSession sqlSession = MybatisUtil.getSqlSession();
AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
List<Admin>admins=adminDao.findAdmins("account");
System.out.println(admins);
sqlSession.close();

五、insert标签的3个属性,获取插入数据的主键值
1、useGeneratedKeys 设置方法是否在此条数据被保存后返回此条数据的主键值
2、keyColumn 设置类中谁来接收主键值
3、keyProperty 设置表中谁为主键(只针对当前返回主键值这个过程/只针对insert标签内的代码)
<insert id="saveAdmin" parameterType="Admin" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into admin(account,password,gender)value(#{account},#{password},#{gender})
</insert>
六、增删改接口
1、添加接口
void saveAdmin(Admin admin);
<!--在insert标签中添加3个属性可以实现获取被保存的那条数据的主键值-->
<insert id="saveAdmin" parameterType="Admin" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into admin(account,password,gender)value(#{account},#{password},#{gender})
</insert>
2、删除接口
void deleteAdmin(int id);
<delete id="deleteAdmin" parameterType="int">
delete from admin where id = #{id}
</delete>
3、修改接口
void updateAdmin(Admin admin);
<update id="updateAdmin" parameterType="Admin">
update admin set account=#{account},password=#{password},gender=#{gender} where id = #{id}
</update>
4、注意
在使用增删改接口时必须使用sqlSession提交事务,而在使用查询接口时可使用sqlSession提交事务,也可不使用sqlSession提交事务,但是最好不使用sqlSession提交事务(因为这样做会浪费资源)
七、单元测试
1、功能:实现对单个方法进行测试
2、配置:
将junit包导入到项目中
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
3、使用:注意:单元测试的方法不是必须是public访问权限修饰符
@Test
public void find(){
System.out.println("你好");
}
八、查询接口
1、返回值为一个值
int findAdminCount();// 查询在册管理员数目
<select id="findAdminCount" resultType="java.lang.Integer">
select count(*) from admin
</select>
注:Java系统为常见的类型起了类型别名,它们不区分大小写(即一个字母的大写和小写是等效的)。



如:select标签里面的resultType的值可以为java.lang.Integer或者Integer或者integer,但是最好为integer(在给resultType赋值系统类统一使用别名,这样更加统一)
<select id="findAdminCount" resultType="Integer">
select count(*) from admin
</select>
<select id="findAdminCount" resultType="integer">
select count(*) from admin
</select>
2、返回值为一个对象
Admin findAdminById(int id);// 根据id值查询指定的数据
<select id="findAdminById" parameterType="int" resultType="Admin">
select * from admin where id = #{id}
</select>
3、返回值为多个对象
List<Admin> findAdmins();// 查询所有数据
<select id="findAdmins" resultType="com.ffyc.mybatispro.model.Admin">
select * from admin
</select>
注意:必须使用集合去接收返回回来的多个对象,否则就会

九、数据库系统实现在单表查询时自动将查询结果中的数据记录封装为一个对象需要注意的事项
1、模型类中一定要有无参构造方法(有参构造方法可有可无),否则就会

2、在模型类中一定要对私有属性全部实现get和set方法
3、数据库表中的列名一定要与对应模型类中的属性名相同(若不相同可通过在sql语句中为列名起别名的方式来处理),否则就会

快捷方式:java中使用标准驼峰命名,数据库中使用下划线连接命名,可以开启全局设置实现自动转换,即就是adminPhone与admin_phone相对应
快捷方式的实现方式:在全局配置中添加此配置
<setting name="mapUnderscoreToCamelCase" value="true"/>
解析:是否开启驼峰命名自动映射,即从经典数据库列名A_COLUMN映射到Java经典属性名a_Column,其默认值是false
十、方法中传递String类型的参数时,必须使用注解标签@Param将String类参数的值与新参数绑定才能将将参数值正确传到sql中,否则就会将传过来的String类参数视为String类中的属性并报错该属性的getter方法不存在

正确做法:
List<Admin> findAdmins(@Param("col") String col);
<select id="findAdmins" resultType="com.ffyc.mybatispro.model.Admin" parameterType="string">
select * from admin order by ${col}
</select>
注意:select标签里面的参数名必须与param标签里面的参数名相同,但是最好的做法是保证param标签、select标签里面的参数名与传递参数的名相同
十一、使用resultMap对查询结果进行自定义映射
1、作用:
处理模型类的属性名与数据库列名不对应的问题
2、使用
<resultMap id="adminMap" type="admin">
<id property="id" column="id"></id>
<result property="account" column="account"></result>
<result property="password" column="password"></result>
<result property="gender" column="gen"></result>
<result property="adminPhone" column="admin_phone"></result>
</resultMap>
<select id="findAdminById" parameterType="int" resultMap="adminMap">
select id,password,account,gen as gender,admin_phone from admin where id = #{id}
</select>
3、解析
(1)resultMap标签中的id属性(是映射的POJO类,其值可自定义)是resultMap的唯一标识,在本例中其值为adminMap,type属性的值select标签中方法的返回值类型
(2)resultMap标签内部的id标签映射sql查询结果中的主键,result标签映射查询结果中的非主键
(3)id标签和result标签中的property属性的值为映射到的类的属性名,column属性的值为查询结果的列名
(4)select标签中必须有resultMap属性引用对应resultMap标签中id属性的值
4、注意
在用到resultMap标签的select标签中使用resultType属性赋值任意的类型都不会对select标签的返回结果造成任何影响,但最好不要去使用resultType(因为使用resultType会浪费资源)
十二、JDBC、MyBatis框架、Hibernate框架对数据库中数据的处理
1、JDBC:
(1)直接使用Java语言对数据库中的数据进行处理
(2)程序员自行封装数据库中的数据进行处理
2、MyBatis框架:
半自动映射,自动映射与自定义映射配合使用,对数据库中的数据进行处理
3、Hibernate框架:
完全自动映射,在创建类时就创建一个与类对应的配置文件,在配置文件中将数据库中的列一一映射到类中的属性上去,从而完成对数据库中的数据的处理

5017

被折叠的 条评论
为什么被折叠?



