Mybatis的学习
2018-10-31 本文已影响0人
Zebraaa
学习mybatis当然配置拉
mybatis-config.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>
<!-- 定义全局变量 连接数据的参数 -->
<properties resource="jdbc.properties">
</properties>
<!-- 全局参数设置 log4j的日志 -->
<settings>
<!-- <setting name="logImpl" value="SLF4J" /> -->
<setting name="logPrefix" value="Mybatis: chen " />
</settings>
<!-- 定义类型别名 bean/pojo类的引入 -->
<typeAliases>
<package name="com.briup.pojo"/>
<package name="com.briup.bean"/>
</typeAliases>
<!-- 定义类型处理器 自定义参数-->
<typeHandlers>
<typeHandler handler="com.briup.handler.PhoneTypeHandler"/>
</typeHandlers>
<!-- 定义数据库连接环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<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>
<!-- mapper接口的扫描 -->
<mappers>
<package name="com/briup/mapper"/>
</mappers>
</configuration>
接口mapper对应的xml
<?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.briup.mapper.StudentMapper">
<!-- <resultMap type="Student" id="StudentWithAddressResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="phone" column="phone" />
<association property="address" resultMap="AddressResult"></association>
</resultMap>
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id" />
<result property="street" column="street" />
<result property="city" column="city" />
<result property="state" column="state" />
<result property="zip" column="zip" />
<result property="country" column="country" />
</resultMap> -->
<!-- <select id="selectAllStudent" resultMap="StudentWithAddressResult">
select * from students
</select>
<select id="selectAddressById" parameterType="int" resultMap="AddressResult" >
select a.addr_id,a.street,a.city,a.state,a.zip,a.country
from addresses a LEFT OUTER JOIN students s
on a.addr_id = s.addr_id
where a.addr_id = #{addr_id}
</select>
<select id="selectAddress" resultMap="StudentWithAddressResult">
select s.STUD_ID,name,EMAIL,phone,CITY,COUNTRY,ZIP from students s
LEFT OUTER JOIN ADDRESSES a on a.ADDR_ID = s.ADDR_ID
</select> -->
<resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id" />
<result property="street" column="street" />
<result property="city" column="city" />
<result property="state" column="state" />
<result property="zip" column="zip" />
<result property="country" column="country" />
</resultMap>
<select id="selectAddress" parameterType="Integer" resultMap="AddressResult">
select * from addresses where addr_id = #{id}
</select>
<resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="phone" column="phone" />
<association property="address" column="addr_id" select="selectAddress"></association>
</resultMap>
<select id="selectoneByone" parameterType="Integer" resultMap="StudentResult">
select * from students where stud_id = #{id}
</select>
</mapper>
对于多个参数的可以用 #{param1}代表第一个参数取值
对于大于小于可以使用 > < 也可以写在
<!-- 如果有特殊符号的话 需要用 <![CDATA[ 特殊符号 ]]> 例如 < & 等等 -->