1,创建一个database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?
useSSL=true&useUnicode=true&charsetEncoding=UTF-8
username=root
password=123456
2,在mybatis核心配置文件中引入properties配置文件,并用${}表达式引入其中的值
<configuration>
<span class="hljs-comment"><!--配置文件修改--></span>
<span class="hljs-tag"><<span class="hljs-name">properties</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">"database.properties"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">environments</span> <span class="hljs-attr">default</span>=<span class="hljs-string">"development"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">environment</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"development"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">transactionManager</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"JDBC"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">dataSource</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"POOLED"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"driver"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${driver}"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"url"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${url}"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${username}"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"${password}"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">dataSource</span>></span>
<span class="hljs-tag"></<span class="hljs-name">environment</span>></span>
<span class="hljs-tag"></<span class="hljs-name">environments</span>></span>
<span class="hljs-tag"><<span class="hljs-name">mappers</span>></span>
<span class="hljs-tag"><<span class="hljs-name">mapper</span> <span class="hljs-attr">resource</span>=<span class="hljs-string">"com/kuang/dao/userMapper.xml"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">mappers</span>></span>
1,配置别名
<typeAliases>
<span class="hljs-comment"><!--配置指定类的别名--></span>
<span class="hljs-tag"><<span class="hljs-name">typeAlias</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"com.kuang.pojo.User"</span> <span class="hljs-attr">alias</span>=<span class="hljs-string">"User"</span>/></span>
2,优化完毕后我们在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.Shandx.dao.UserDao">
<span class="hljs-tag"><<span class="hljs-name">select</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"selectUser"</span> <span class="hljs-attr">resultType</span>=<span class="hljs-string">"User"</span>></span>
select * from user
<span class="hljs-tag"></<span class="hljs-name">select</span>></span>
字段名对应的是数据库,属性名对应的是实体类,如果我们的字段名和属性名不一致时,我们就获取不到数据;譬如:
那么属性名和字段名不同的值将获取不到
**那么解决方法有三种
第一种是修改set方法名字,我们不推荐使用
第二种是给sql语句取别名,但是只是在字段少的时候使用**
第三种方法是结果集映射ResultMap ,这个方法我们推荐使用
【前提是我们要在配置好指定类的别名】
<!--设置结果的映射类型-->
<resultMap id="UserMap" type="User">
<!--
一般通过id标签来映射主键
column = 数据库的列名
property = 结果集对应的数据库列名的映射名
-->
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
手机扫一扫
移动阅读更方便
你可能感兴趣的文章