XML文档配置
<?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>
<!--通过属性可以将值进行外部配置,从而实现动态替换 -->
<!--mysql.properties
username = dev_user
password = F2Fa3!33TYyg -->
<properties resource="mysql.properties"/>
<settings>
<!-- 开启日志-->
<setting name="logImpl" value="LOG4J"/>
<!--驼峰命名映射,userName可以对应user_name-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 为 Java 类型设置全局的缩写名字,为其他配置文件降低冗余的全限定类名书写-->
<typeAliases>
<!-- 包扫描,默认把包下的所有类都赋予一个Bean首字母小写的别名-->
<package name="com.hjc.POJO"/>
<typeAlias alias="Blog" type="domain.blog.Blog"/>
</typeAliases>
<!--尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境,通过构造函数参数选择environment的id,否则使用default的environment-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!---告诉 MyBatis 去哪里找映射文件-->
<!-- 一般会在resource下建一个对应接口所在包的文件夹-->
<mappers>
<mapper resource="com/hjc/mapper/UserMapper.xml"/>
<mapper resource="com/hjc/mapper/PhoneMapper.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">
<!--namespace:对应的接口文件-->
<mapper namespace="com.hjc.mapper.UserMapper">
<!--id:对应接口的函数方法-->
<update id="updateUser">
<!--传参方法:
@Param("userid") int userid
@Param("user") User user ,使用的时候#{user.id}-->
update mybatis.user set name = #{name},pwd=#{pwd} where id = #{id};
</update >
<delete id="deleteUser" >
delete from mybatis.user where id = #{id};
</delete >
<insert id="insertUser" >
insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});
</insert >
<!-- resultType:传入类名,并要求类属性和数据库列名要一一对应否则取值为Null-->
<select id="getUserByLimit" resultType="user">
select * from mybatis.user limit ${start},${number};
</select>
<!-- resultMap:自定义属性和数据库列名的对应关系-->
<!-- id:标识符,用于别人通过标识符引用
type:该map对应的java类型-->
<resultMap id="userMap" type="user">
<!--id:通常对应主键,写一个id往往会提升速度
property:变量名
column:数据库列名-->
<id property="id" column="uid"/>
<result property="name" column="tname"/>
<result property="pwd" column="pwd"/>
<!-- 多对一-->
<!--ofType:集合中的类型名,它没有javaType-->
<collection property="phones" ofType="phone" resultMap="phoneMap"/>
</resultMap>
<resultMap id="phoneMap" type="phone">
<id property="id" column="pid"/>
<result property="name" column="pname"/>
<result property="price" column="price"/>
</resultMap>
<select id="getUserList" resultMap="userMap">
select p.id as pid,p.name pname,p.price,u.id as uid ,u.name tname,u.pwd
from user u left outer join phone p on p.userid = u.id;
</select>
<!-- 一对多 -->
<resultMap id="phoneMap" type="phone">
<id property="id" column="pid"/>
<result property="name" column="pname"/>
<result property="price" column="price"/>
<association property="user" javaType="user">
<result property="name" column="pname"></result>
<result property="pwd" column="pwd"></result>
</association>
</resultMap>
<select id="getPhones" resultMap="phoneMap">
select p.id as pid,p.name pname,p.price,u.id as uid ,u.name tname,u.pwd
from phone p left outer join user u on p.userid = u.id;
</select>
</mapper>