Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式
resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置
1、項目目錄
2、數(shù)據(jù)庫中的表的信息
3、配置文件的信息
1、SqlMapConfig.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"> <!--mybatis主配置文件--> <configuration> <!--配置環(huán)境--> <environments default="mysql"> <!-- 配置mysql環(huán)境--> <environment id="mysql"> <!-- 配置事務(wù)類型--> <transactionManager type="JDBC"></transactionManager> <!-- 配置數(shù)據(jù)源(連接池)--> <dataSource type="POOLED"> <!-- 配置數(shù)據(jù)庫的基本信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT"/> <property name="username" value="root"/> <property name="password" value="111111"/> </dataSource> </environment> </environments> <!-- 指定映射配置文件的位置,映射配置文件指的是每一個dao獨立的配置文件--> <mappers> <mapper resource="com/mybatis/dao/IUserDao.xml"/> </mappers> </configuration>
2、IUserDao.xml
其中的mapper標(biāo)簽中的namespace屬性指的就是持久層中的接口,這里的sql語句都是對應(yīng)這個接口中的方法,也就是指定了命名空間。
在這里resultMap標(biāo)簽是查詢結(jié)果的列名和實體類的屬性名的對應(yīng)關(guān)系,也就是說我們類中的屬性名不一定和數(shù)據(jù)庫中的保持一致,其中property配置的就是類中的屬性名,column設(shè)置的就是數(shù)據(jù)庫中表的字段名。
在sql語句的標(biāo)簽中之前的,resultType變成了resultMap。sql標(biāo)簽中直接寫的是就是sql語句,這個可以有效的避免重復(fù)的寫sql相同代碼,如果要引用sql標(biāo)簽中內(nèi)容,在對應(yīng)的語句中需要引用Include標(biāo)簽,具體的可以看下面的代碼。
<?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.mybatis.dao.IUserDao"> <!-- 配置,查詢結(jié)果的列名和實體類的屬性名的對應(yīng)關(guān)系--> <resultMap id="userMap" type="com.mybatis.domain.User"> <!-- 主鍵字段對應(yīng)--> <id property="userId" column="id"></id> <!-- 非主鍵字段對應(yīng)--> <result property="userName" column="username"></result> <result property="userAddress" column="address"></result> <result property="userSex" column="sex"></result> <result property="userBirthday" column="birthday"></result> </resultMap> <sql id="defaultUser"> select * from users </sql> <!-- 查詢所有--> <select id="findAll" resultMap="userMap"> <include refid="defaultUser"></include> </select> <select id="findById" parameterType="INT" resultMap="userMap"> select * from users where id = #{uid} </select> </mapper>
4、User類
package com.mybatis.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private Integer userId; private String userName; private Date userBirthday; private String userSex; private String userAddress; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Date getUserBirthday() { return userBirthday; } public void setUserBirthday(Date userBirthday) { this.userBirthday = userBirthday; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this.userAddress = userAddress; } @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", userBirthday=" + userBirthday + ", userSex='" + userSex + '\'' + ", userAddress='" + userAddress + '\'' + '}'; } }
5、IUserDao接口
package com.mybatis.dao; import com.mybatis.domain.User; import java.util.List; public interface IUserDao { /** * 查詢所有用戶 * @return */ List<User> findAll(); /** * 根據(jù)ID查詢用戶信息 * @param userId * @return */ User findById(Integer userId); }
6、MybatisTest
package com.mybatis.test; import com.mybatis.dao.IUserDao; import com.mybatis.domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.List; public class MybatisTest { private InputStream in; private SqlSession session; private IUserDao userDao; @Before public void init() throws Exception { this.in = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder(); System.out.println(in); SqlSessionFactory factory = factoryBuilder.build(in); // this.session = factory.openSession(true); this.session = factory.openSession(); this.userDao = session.getMapper(IUserDao.class); } @After public void destory() throws IOException { session.commit(); this.in.close(); this.session.close(); } @Test public void testFindAll() throws Exception{ List<User> users = userDao.findAll(); for (User user:users){ System.out.println(user); } } }
7、運行結(jié)果
resultMap標(biāo)簽的使用規(guī)則
自定義結(jié)果映射規(guī)則
<!-- resultMap自定義某個javabean的封裝規(guī)則 ? ? ? ?type:自定義規(guī)則的java類型 ? ? ? ?id:唯一id方便引用 ? ? ?--> ? ? <resultMap type="entity.Employee" id="getEmpByIdMap"> ? ? ? ?<!-- id指定主鍵列的封裝規(guī)則 ? ? ? ? ? ?column:指定哪一列 ? ? ? ? ? ?property:指定對應(yīng)的javabean屬性 ? ? ? ? --> ? ? ? ?<id column="id" property="id"/> ? ? ? ?<!-- result定義普通列封裝規(guī)則,若屬性名與數(shù)據(jù)庫對應(yīng)表的列名相同可不寫, ? ? ? ? ? ? mybatis會自動封裝,但建議將所有的映射規(guī)則都寫上 ? ? ? ?--> ? ? ? ?<result column="name" property="name"/> ? ? ? ?<result column="sex" property="sex"/> ? ? ? ?<result column="email" property="email"/> ? ? </resultMap> ? ? <!-- public Employee getEmpById(Integer id) --> ? ? <select id="getEmpById" resultMap="getEmpByIdMap"> ? ? ? ?select * from employee where id=#{id} ? ? </select>
association聯(lián)合查詢
association可以指定聯(lián)合的javabean對象
property="dept"
:指定哪個屬性是聯(lián)合對象javaType
:指定這個屬性的類型
<resultMap type="entity.Employee" id="getEmpAndDeptMap"> ? ? ? ?<id column="id" property="id"/> ? ? ? ?<result column="empName" property="name"/> ? ? ? ?<result column="sex" property="sex"/> ? ? ? ?<result column="email" property="email"/> ? ? ? ?<!-- association可以指定聯(lián)合的javabean對象 ? ? ? ? ? ? property="dept":指定哪個屬性是聯(lián)合對象 ? ? ? ? ? ? javaType:指定這個屬性的類型--> ? ? ? ?<association property="dept" javaType="entity.Department"> ? ? ? ? ? ?<id column="did" property="id"/> ? ? ? ? ? ?<result column="deptName" property="departmentName"/> ? ? ? ?</association> ? ? </resultMap> ? ? <!-- public Employee getEmpAndDept(Integer id) --> ? ? <select id="getEmpAndDept" resultMap="getEmpAndDeptMap"> ? ? ? ?select e.id id,e.name empName,e.email email,e.sex sex,e.d_id d_id, ? ? ? ? ? ?d.id did,d.name deptName from employee e,dept d ? ? ? ? ? ?where e.d_id=d.id and e.id=#{id} ? ? </select>
使用association進(jìn)行分布查詢
1、先按照員工id查詢員工信息將會調(diào)用查詢員工的sql
2、根據(jù)查詢員工信息中的d_id值去部門表中查出部門信息
3、部門設(shè)置到員工中
<resultMap type="entity.Employee" id="getEmpAndDeptStepMap"> ? ? ? ?<id column="id" property="id"/> ? ? ? ?<result column="name" property="name"/> ? ? ? ?<result column="sex" property="sex"/> ? ? ? ?<result column="email" property="email"/> ? ? ? ?<!-- association定義關(guān)聯(lián)對象的封裝規(guī)則 ? ? ? ? ? ? select:表明當(dāng)前屬性是調(diào)用select指定的方法查出的結(jié)果 ? ? ? ? ? ? column:指定將那一列的值作為參數(shù)傳給這個方法 ? ? ? ? ? ? ?流程:使用select指定的方法(傳入column指定的這列參數(shù)的值)查出對象, ? ? ? ? ? ? ?并封裝給property指定的屬性 ? ? ? ? ? ? --> ? ? ? ? ? ? <!-- discriminator鑒別器 ? ? ? ? ? ? ? ? ?column:指定判定的列名 ? ? ? ? ? ? ? ? ?javaType:列值對應(yīng)的java類型 ? ? ? ? ? ? ?--> ? ? ? ?<discriminator javaType="string" column="sex"> ? ? ? ? ? ?<!-- resultType不能缺少 --> ? ? ? ? ? ?<case value="男" resultType="entity.Employee"> ? ? ? ? ? ? ? <association property="dept" select="dao.DepartmentMapper.getDeptById" ? ? ? ? ? ? ? ? ? column="d_id"> ? ? ? ? ? ? ? </association> ? ? ? ? ? ?</case> ? ? ? ?</discriminator> ? ? </resultMap> ? ? <!-- public Employee getEmpByIdStep(Integer id) --> ? ? <select id="getEmpByIdStep" resultMap="getEmpAndDeptStepMap"> ? ? ? ?select * from employee where id=#{id} ? ? </select>
嵌套結(jié)果集的方式,使用collection標(biāo)簽定義關(guān)聯(lián)的集合類型的屬性封裝規(guī)則
<resultMap type="entity.Department" id="getDeptByIdPlusMap"> ? ? ? ?<id column="did" property="id"/> ? ? ? ?<result column="deptName" property="departmentName"/> ? ? ? ?<!-- collection定義關(guān)聯(lián)集合類型的屬性的封裝規(guī)則 ? ? ? ? ? ? ofType:指定集合里面元素的類型 ? ? ? ? ? ?? ? ? ? ? --> ? ? ? ?<collection property="emps" ofType="entity.Employee"> ? ? ? ? ? ?<!-- 定義這個集合中元素的封裝規(guī)則 --> ? ? ? ? ? ?<id column="eid" property="id"/> ? ? ? ? ? ?<result column="empName" property="name"/> ? ? ? ? ? ?<result column="sex" property="sex"/> ? ? ? ? ? ?<result column="email" property="email"/> ? ? ? ?</collection> ? ? </resultMap> ? ? <!-- public Department getDeptByIdPlus(Integer id) --> ? ? <select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap"> ? ? ? ?select d.id did,d.name deptName,e.id eid, ? ? ? ? ? ?e.name empName,e.sex,e.email ? ? ? ? ? ?from dept d left join employee e ? ? ? ? ? ?on d.id=e.d_id ? ? ? ? ? ?where d.id=#{id} ? ? </select>
collection分步查詢
<resultMap type="entity.Department" id="getDeptByIdStepMap"> ? ? ? ?<id column="id" property="id"/> ? ? ? ?<result column="name" property="departmentName"/> ? ? ? ?<collection property="emps" select="dao.EmployeeMapperPlus.getEmpsByDeptId" ? ? ? ? ? ?column="{id}"> ? ? ? <!-- 或則 column="{deptId=id}"--> ? ? ? ?</collection> ? ? </resultMap> ? ?<!-- public List<Employee> getEmpsByDeptId(Integer deptId --> ? ?<select id="getEmpsByDeptId" resultType="entity.Employee"> ? ? ? ?select * from employee where d_id=#{deptId} ? ? </select> ? ? <!-- public Department getDeptByIdStep(Integer id) --> ? ? <select id="getDeptByIdStep" resultMap="getDeptByIdStepMap"> ? ? ? ?select * from dept where id=#{id} ? ? </select>
當(dāng)分布查詢需要傳遞多個多個值時,將多個值封裝map傳遞
colum=“{key1=column1,key2=colum2...}”
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java中將一個List等分成n個list的工具方法(推薦)
下面小編就為大家?guī)硪黄猨ava中將一個List等分成n個list的工具方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03java編程下字符串的16位,32位md5加密實現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava編程下字符串的16位,32位md5加密實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09logback的isDebugEnabled日志配置級別源碼解析
這篇文章主要為大家介紹了logback的isDebugEnabled日志配置級別源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11JDBC PreparedStatement Like參數(shù)報錯解決方案
這篇文章主要介紹了JDBC PreparedStatement Like參數(shù)報錯解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10