发现问题:
虽然在MyBatis狂神版中对之前的操作进行了升级改造,但是在使用中发现了一些问题,所以对其进行升级改造
通过properties属性来实现引用配置文件
之前的配置
<?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核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
</configuration>
在之前的配置中,我们将
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?
useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
中的有关数据库中的信息写死了,这样出现了一个问题,我如果想换库怎么办?是不是又要对代码进行改动?
因此我们引用配置文件的方法来解决这一尴尬的问题:
第一步:在resource包下建立一个.properties文件:
我命名为:db.properties,里头写好数据库相关的信息(就是之前在代码中写死的那部分)
第二步:引入外部配置文件
在核心配置文件中添加
<!--引入外部配置文件-->
<properties resource="db.properties"/>
或者采用配置文件与属性相结合的方式,将部分写死:
<!--引入外部配置文件-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="pwd" value="11111"/>
</properties>
给类起别名
之前的使用中,我们在写SQL语句时,需要将类的全路径写入,这样增加了许多无用的操作,十分麻烦,所以我们使用给类起别名这一办法进行优化:
方式一:适用于实体类较少的情况
在核心配置文件中加入
<!--可以给实体类起别名-->
<typeAliases>
<typeAlias type="com.kuang.pojo.User" alias="User"/>
</typeAliases>
逐个给实体类起别名
在Mapper.xml中的SQL则为:
<!--select查询语句-->
<select id="getUserList" resultType="User">
select * from user
</select>
方式二:适用于实体类较多的情况
<!--可以给实体类起别名-->
<typeAliases>
<package name="com.kuang.pojo"/>
</typeAliases>
将这个com.kuang.pojo包下的所有实体类起成小写的别名,在Mapper.xml中的SQL则为:
<!--select查询语句-->
<select id="getUserList" resultType="user">
select * from user
</select>
方式二如果需要进行DIY起别名则需要在实体类中加入注解来进行:@Alias(“DIY的名字”)
在Mapper.xml中的SQL则为:
<!--select查询语句-->
<select id="getUserList" resultType="Myuser">
select * from user
</select>
骚操作:万能Map
假设,我们的实体类,或者数据库中的表,字段或者参数过多,而我们只需要对个别字段进行操作,这是我们需要将繁琐的SQL语句进行一遍遍的写,因此我们应当使用万能的Map!
第一步:在Dao层中的Mapper接口里编写方法
以插入为例:
//万能的Map
int addUser2(Map<String,Object> map);
第二步:在Dao层中的Mapper.xml中实现SQL:
<!--对象中的属性,可以直接取出来 传递map的key-->
<insert id="addUser" parameterType="map">
insert into mybatis.user (id, pwd) values (#{userid},#{passWord});
</insert>
其中#{userid}为自己DIY的名字
Map传递参数,直接在sql中取出key即可! 【parameterType=“map”】
对象传递参数,直接在sql中取对象的属性即可!【parameterType=“Object”】
第三步:测试
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userid",5);
map.put("passWord","2222333");
mapper.addUser2(map);
sqlSession.close();
}
Map传递参数,直接在sql中取出key即可! 【parameterType=“map”】
对象传递参数,直接在sql中取对象的属性即可!【parameterType=“Object”】
只有一个基本类型参数的情况下,可以直接在sql中取到!
多个参数用Map,或者注解!