Mybatis—③优化代码

一、优化配置文件

1、创建一个database.properties文件
放在resources下

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

2、在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>
    <properties resource="database.properties"/>
    <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="com/an/dao/UserMapper.xml"/>
    </mappers>
</configuration>
二、优化别名

可以配置指定类的别名<typeAlias type="com.kuang.pojo.User" alias="User"/>
可以为一个包的所有类指定别名,这个别名为类名<package name="com.kuang.pojo"/>
这里我们直接用第二种方法,直接为一个包里所有类取别名为类名

<?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>
    <properties resource="database.properties"/>
    <!--为一个包里所有类取别名为类名,注意位置为固定的-->
    <typeAliases>
        <package name="com.an.pojo"/>
    </typeAliases>
    <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="com/an/dao/UserMapper.xml"/>
    </mappers>
</configuration>
三、然后我们在mapper映射文件中就可以使用别名了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.an.dao.UserDao">
    <select id="selectUser" resultType="User">
        select * from user;
    </select>

    <select id="selectUserById" resultType="User">
        select * from user where id = #{id}
    </select>

    <!--接受一个自定义对象(引用对象)时,要设置parameterType,为参数类型接收
    这个对象的值,直接使用#{对象字段名}-->
    <insert id="addUser" parameterType="User">
        insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
    </insert>

    <delete id="deleteUserById" parameterType="int">
        delete from user where id = #{id}
    </delete>

    <update id="updateUser" parameterType="User">
        update user set name = #{name},pwd = #{pwd} where id = #{id}
    </update>

</mapper>

映射器推荐使用resource,使用相当于类路径资源的引用

<mappers>
        <mapper resource="com/an/dao/UserMapper.xml"/>
    </mappers>

解决属性名和字段名不一致的问题

我们用来做练习的这个数据库user表
修改为如下
数据库字段名为 id name pwd
java实体类属性名为id name password
修改完毕然后去测试一下
查询所有的用户

User{id=1, name='anye', password='null'}
User{id=2, name='张三', password='null'}
User{id=3, name='李四', password='null'}
User{id=4, name='qinjiang', password='null'}
User{id=5, name='安夜', password='null'}
User{id=6, name='anye', password='null'}

不一致的密码查询出来都是null,我们来分析一下
select * from user 等价于

select id,name,pwd from user

mybatis会根据数据库的字段名去找对应的实体类的属性名,(他会将所有列名转换为小写,然后去找实体类中对应的 set方法 ,set方法后面的字段就对应数据库的字段名;如果不一样就会返回null)
解决方法
方法一修改set方法名

public void setPwd(String pwd) {
        this.password = pwd;
    }

这样set后的字段就与数据库字段名一致了,但不建议使用这种方法
方法二给sql语句取别名

        select id,name,pwd as password from user;

这个看的出来很麻烦,同样不建议使用
方法三结果集映射ResultMap,这个推荐使用

<mapper namespace="com.an.dao.UserDao">
    <!--设置结果的映射类型-->
    <resultMap id="UserMap" type="User">
        <!--
        一般通过id标签来映射主键
            column = 数据库的列名
            property = 结果集对应的数据库列名的映射名
        -->
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="pwd" property="password"/>
    </resultMap>
    
    <!--resultMap与上面设置的id类型一致-->
    <select id="selectUser" resultMap="UserMap">
        select * from user;
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值