03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma



1 PersonTestMapper.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">

<!--

namespace:命名空间,用来唯一标识一个映射文件,命名规范就是当前的文件的包加上文件名

-->

<mapper
namespace="com.rl.mapper.PersonTestMapper">

<!--

根据id来查询一个Person的数据

sql语句接收参数的一个语法#{},如果接收的是一个{}中的内容任意select
* from person_test t where t.ID = ?,使用预编译方式生成sql

id:sql语句的唯一的标识不能重复

parameterType:sql要接收的数据类型

resultType:sql所返回的数据类型

-->

<!--

实际项目中数据库的表的字段一般由多个单词来构成
由下划线来分隔多个单词 person_addr

在java的model的实体类中的属性多个单词的命名规范是驼峰模式personAddr

-->

<select
id="selectPersonById"
parameterType="java.lang.Integer"
resultType="com.rl.model.Person">

select * from person_test t where t.ID = #{id}

</select>

</mapper>


PersonMapper.xml的配置内容如下(resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询map传递参数,模糊查询,插入,更新,删除,where条件查询,动态修改,in查询foreach迭代,批量插入foreach,批量删除,一对多查询,extends:resultMap的继承,多对多查询,延迟加载):


<?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:命名空间,用来唯一标识一个映射文件,命名规范就是当前的文件的包名+mapperxml文件名

-->

<mapper
namespace="com.rl.mapper.PersonMapper">

<!--当前映射文件开启二级缓存-->

<cache
type="org.mybatis.caches.ehcache.EhcacheCache"/>

<!--

id:resultMap的唯一标识

type:给哪个实体类做结果的映射

-->

<resultMap
type="person"
id="BaseResultMap">

<!--

column:数据库中表的字段

property:数据库中表所有映射的实体类javaBean中的属性名

-->

<id
column="person_id"
property="personId"/>

<result
column="name"
property="name"/>

<result
column="gender"
property="gender"/>

<result
column="person_addr"
property="personAddr"/>

<result
column="birthday"
property="birthday"/>

</resultMap>

<!--

公用的sql片段,也可以接收参数,动态sql,所有的sql可以使用

-->

<sql
id="columns">

PERSON_ID, NAME, GENDER, PERSON_ADDR, BIRTHDAY

</sql>

<!--

根据id来查询一个Person的数据

sql语句接收参数的一个语法#{},如果接收的是一个{}中的内容任意select
* from person_test t where t.ID = ?,使用预编译方式生成sql

id:sql语句的唯一的标识不能重复

parameterType:sql要接收的数据类型

resultType:sql所返回的数据类型

-->

<!--

实际项目中数据库的表的字段一般由多个单词来构成
由下划线来分隔多个单词 person_addr

在java的model的实体类中的属性多个单词的命名规范是驼峰模式personAddr

-->

<!--

useCache:控制当前的这个sql是否使用二级缓存

-->

<select
id="selectPersonById"
parameterType="int"
resultMap="BaseResultMap"
useCache="true">

select * from person t where t.person_id = #{id}

</select>

<select
id="selectPersonCount"
resultType="int">

select count(*) from person

</select>

<!--
这里引用了上面的sql片段
-->

<select
id="selectPersonAll"
resultMap="BaseResultMap">

select <include
refid="columns"/> from person

</select>

<!--

可以使用map

map.put("gender",1);

map.put("birthday" new Date());

#{}中的内容使用Map的key来接收参数

-->

<select
id="selectPersonByParams"
parameterType="map"
resultMap="BaseResultMap">

<![CDATA[

select * from person t where t.gender = #{gender} and t.birthday < #{birthday}

]]>

</select>

<!--

使用查询对象的get方法来接收参数(也就是属性名)

-->

<select
id="selectPersonByParams1"
parameterType="qc"
resultMap="BaseResultMap">

<![CDATA[

select * from person t where t.gender = #{gender} and t.birthday < #{birthday}

]]>

</select>

<!--

模糊查询使用${} select * from person t
where t.name like ‘%安%‘

parameterType:不能直接使用String,一定要用查询对象或者map

-->

<select
id="selectPersonByLike"
parameterType="qc"
resultMap="BaseResultMap">

select * from person t where t.name like ‘%${name}%‘

</select>

<!--
库表变更 -->

<insert
id="insert"
parameterType="person">

<!--

keyProperty:实体类中主键属性,主键生成后把主键返回给这个属性

order:生成主键的sql和insert执行的顺序,
mysql是AFTER,
oracle是BEFORE

resultType: 主键返回的数据类型

sql

mysql中select
LAST_INSERT_ID()

在oracle中
select xxx.nextval from dual

selectKey做了两件事:1.主键自增
2.主键返回

-->

<selectKey
keyProperty="personId"
order="AFTER"
resultType="int">

select LAST_INSERT_ID()

</selectKey>

insert into person (person_id, name, gender, person_addr, birthday)

values(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday})

</insert>

<update
id="update"
parameterType="person">

update person p set p.name = #{name},

p.gender = #{gender},

p.person_addr = #{personAddr},

p.birthday = #{birthday}

where p.person_id = #{personId}

</update>

<!--

删除的sql不能使用别名

-->

<delete
id="delete"
parameterType="int">

delete from person where person_id = #{personId}

</delete>

<!-- =============================动态sql==================================
-->

<!--

map.put("name", "安");

map.put("gender", "0");

map.put("personAddr", "东京")

map.put("birthday", new Date());

<where>会自动处理and,
第一个and可以不写,其他的and必须要写

-->

<select
id="selectPersonByCondition"
parameterType="map"
resultMap="BaseResultMap">

select * from person t

<where>

<if
test="name != null">

t.name like ‘%${name}%‘

</if>

<if
test="gender != null">

and t.gender = #{gender}

</if>

<if
test="personAddr != null">

and t.person_addr like ‘%${personAddr}%‘

</if>

<if
test="birthday != null">

<![CDATA[

and t.birthday < #{birthday}

]]>

</if>

</where>

</select>

<!--

动态修改

<set>标签可以去掉最后一个逗号

flushCache:二级缓存的刷新的配置:默认是true:会刷新,如果false就不刷新缓存

-->

<update
id="dynamicUpdate"
parameterType="person"
flushCache="false">

update person t

<set>

<if
test="name != null">

t.name = #{name},

</if>

<if
test="gender != null">

t.gender = #{gender},

</if>

<if
test="personAddr != null">

t.person_addr = #{personAddr},

</if>

<if
test="birthday != null">

t.birthday = #{birthday}

</if>

</set>

where t.person_id = #{personId}

</update>

<!--

select * from person t where t.person_id in (1,2,3)

map.put("ids", list);

-->

<select
id="selectPersonByIn"
parameterType="map"
resultMap="BaseResultMap">

select * from person t where t.person_id in

<foreach
collection="ids"
item="personId"
open="("
close=")"
separator=","
index="index">

#{personId}

</foreach>

</select>

<!--

map.put("pList", pList);

insert into person (person_id, name, gender, person_addr, birthday)

values

(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday}),

(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday}),

(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday}),

(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday}),

(#{personId}, #{name}, #{gender}, #{personAddr}, #{birthday});

-->

<insert
id="insertBatch"
parameterType="map">

<selectKey
keyProperty="personId"
order="AFTER"
resultType="int">

select LAST_INSERT_ID()

</selectKey>

insert into person (person_id, name, gender, person_addr, birthday)

values

<foreach
collection="pList"
item="person"
separator=",">

(#{person.personId}, #{person.name}, #{person.gender}, #{person.personAddr}, #{person.birthday})

</foreach>

</insert>

<delete
id="deleteBatch"
parameterType="map">

delete from person where person_id in

<foreach
collection="ids"
item="personId"
open="("
close=")"
separator=","
index="index">

#{personId}

</foreach>

</delete>

<!-- ===============================关联查询==================
-->

<!--
一对多 -->

<resultMap
type="person"
id="selectPersonAndOrderByPIdRM">

<id
column="person_id"
property="personId"/>

<result
column="name"
property="name"/>

<result
column="gender"
property="gender"/>

<result
column="person_addr"
property="personAddr"/>

<result
column="birthday"
property="birthday"/>

<!--

collection:一对多的关联映射

property:一的端集合的属性名

ofType:集合中的泛型

-->

<collection
property="ordersList"
ofType="com.rl.model1.Orders">

<id
column="ORDER_ID"
property="orderId"
jdbcType="INTEGER"
/>

<result
column="PERSON_ID"
property="personId"
jdbcType="INTEGER"
/>

<result
column="TOTAL_PRICE"
property="totalPrice"
jdbcType="REAL"
/>

<result
column="ADDR"
property="addr"
jdbcType="VARCHAR"
/>

</collection>

</resultMap>

<!--

extends:resultMap的继承

-->

<resultMap
type="person"
id="selectPersonAndOrderByPIdRM1"
extends="BaseResultMap">

<collection
property="ordersList"
ofType="com.rl.model1.Orders">

<id
column="ORDER_ID"
property="orderId"
jdbcType="INTEGER"
/>

<result
column="PERSON_ID"
property="personId"
jdbcType="INTEGER"
/>

<result
column="TOTAL_PRICE"
property="totalPrice"
jdbcType="REAL"
/>

<result
column="ADDR"
property="addr"
jdbcType="VARCHAR"
/>

</collection>

</resultMap>

<resultMap
type="person"
id="selectPersonOrderAndDetailByPIdRM"
extends="BaseResultMap">

<collection
property="ordersList"
ofType="com.rl.model1.Orders">

<id
column="ORDER_ID"
property="orderId"
jdbcType="INTEGER"
/>

<result
column="PERSON_ID"
property="personId"
jdbcType="INTEGER"
/>

<result
column="TOTAL_PRICE"
property="totalPrice"
jdbcType="REAL"
/>

<result
column="ADDR"
property="addr"
jdbcType="VARCHAR"
/>

<collection
property="detailList"
ofType="com.rl.model1.OrderDetail">

<id
column="DETAIL_ID"
property="detailId"
jdbcType="INTEGER"
/>

<result
column="ORDER_ID"
property="orderId"
jdbcType="INTEGER"
/>

<result
column="PRICE"
property="price"
jdbcType="REAL"
/>

<result
column="QUANTITY"
property="quantity"
jdbcType="INTEGER"
/>

<result
column="ITEM_NAME"
property="itemName"
jdbcType="VARCHAR"
/>

</collection>

</collection>

</resultMap>

<resultMap
type="person"
id="selectPersonAndRoleByPIdRM"
extends="BaseResultMap">

<collection
property="roleList"
ofType="com.rl.model1.Role">

<id
column="ROLE_ID"
property="roleId"
jdbcType="INTEGER"
/>

<result
column="ROLE_NAME"
property="roleName"
jdbcType="VARCHAR"
/>

<result
column="DESCRIPT"
property="descript"
jdbcType="VARCHAR"
/>

</collection>

</resultMap>

<select
id="selectPersonAndOrderByPId"
parameterType="int"
resultMap="selectPersonAndOrderByPIdRM1">

select * from person p, orders o where p.PERSON_ID = o.PERSON_ID and p.PERSON_ID = #{personId}

</select>

<select
id="selectPersonOrderAndDetailByPId"
parameterType="int"
resultMap="selectPersonOrderAndDetailByPIdRM">

select * from person p,

orders o,

order_detail od where

p.PERSON_ID = o.PERSON_ID

and o.ORDER_ID = od.ORDER_ID

and p.PERSON_ID = #{personId}

</select>

<!--
多对多从Person一端看
-->

<select
id="selectPersonAndRoleByPId"
parameterType="int"
resultMap="selectPersonAndRoleByPIdRM">

SELECT p.*, r.* from person p,

person_role pr,

role r where

p.PERSON_ID = pr.PERSON_ID

and pr.ROLE_ID = r.ROLE_ID

and p.PERSON_ID = #{personId}

</select>

<!-- =========================延迟加载========================
-->

<resultMap
type="person"
id="selectPersonByIdLazyRM"
extends="BaseResultMap">

<!--

column:主sql的一列作为子sql的参数

select:指定子sql的位置

-->

<collection
property="ordersList"
column="person_id"
select="com.rl.mapper.OrdersMapper.selectOrderByPersonId">

</collection>

</resultMap>

<select
id="selectPersonByIdLazy"
parameterType="int"
resultMap="selectPersonByIdLazyRM">

select * from person t where t.person_id = #{personId}

</select>

</mapper>

3
配置sqlMapConfig.xml中的<mappers>配置:


<!--
集中管理表的映射文件 -->

<mappers>

<mapper
resource="com/rl/mapper/PersonTestMapper.xml"
/>

<mapper
resource="com/rl/mapper/PersonMapper.xml"
/>

<mapper
resource="com/rl/mapper/RoleMapper.xml"
/>

<mapper
resource="com/rl/mapper/OrdersMapper.xml"
/>

<mapper
resource="com/rl/mapper/OrderDetailMapper.xml"
/>

</mappers>

4
测试代码如下:

MybatisTest.java


package com.rl.test;

import java.io.InputStream;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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.Before;

import org.junit.Test;

import com.rl.model1.Person;

import com.rl.model1.QueryCondition;

/**

* mybatis的简单查询

*/

public class MybatisTest {

SqlSessionFactory sessionFactory;

@Before

public void setUp() throws Exception {

InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");

sessionFactory = new SqlSessionFactoryBuilder().build(in);

}

/**

* 通过id查询数据

*/

@Test

public void selectPersonById() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

com.rl.model.Person person  = session.selectOne(

"com.rl.mapper.PersonTestMapper.selectPersonById", 1);

System.out.println(person);

} finally{

session.close();

}

}

/**

* 使用resultMap来查询

*/

@Test

public void selectPersonById1() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

Person person  = session.selectOne("com.rl.mapper.PersonMapper.selectPersonById", 1);

System.out.println(person);

} finally{

session.close();

}

}

/**

* 查询表的记录数

*/

@Test

public void selectPersonCount() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Integer count = session.selectOne("com.rl.mapper.PersonMapper.selectPersonCount");

System.out.println(count);

} finally{

session.close();

}

}

/**

* 查询所有Person

*/

@Test

public void selectPersonAll() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//查询集合的时候需要使用selectList

List<Person> pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonAll");

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

/**

* 使用多个参数用Map方式来查询

*/

@Test

public void selectPersonByParams() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Map<String, Object> map = new HashMap<String, Object>();

map.put("gender",0);

map.put("birthday", new Date());

//查询集合的时候需要使用selectList

List<Person> pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByParams",map);

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

/**

* 使用多个参数查询数据,使用查询对象的方式

*/

@Test

public void selectPersonByParams1() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

QueryCondition qc = new QueryCondition();

qc.setGender("0");

qc.setBirthday(new Date());

//查询集合的时候需要使用selectList

List<Person> pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByParams1",qc);

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

/**

* 模糊查询

*/

@Test

public void selectPersonByLike() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

QueryCondition qc = new QueryCondition();

qc.setName("武");

//查询集合的时候需要使用selectList

List<Person> pList = session.selectList("com.rl.mapper.PersonMapper.selectPersonByLike",qc);

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

}

MybatisTest1.java的内容如下:


package com.rl.test;

import java.io.InputStream;

import java.util.Date;

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.Before;

import org.junit.Test;

import com.rl.model1.Person;

/**

* mybatis的数据库表的变更

*/

public class MybatisTest1 {

SqlSessionFactory sessionFactory;

@Before

public void setUp() throws Exception {

InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");

sessionFactory = new SqlSessionFactoryBuilder().build(in);

}

/**

* 数据插入主键返回

* public void saveOrder(Orders order, List<OrderDetail> detailList){

orderDao.save(order);

for(OrderDetail detail : detailList){

detail.setOrderId(order.getOrderId());

detailDao.save(detail)

}

}

*/

@Test

public void insert(){

SqlSession session = sessionFactory.openSession();

Person p = new Person();

//p.setPersonId(3);

p.setName("武松");

p.setGender("0");

p.setPersonAddr("阳谷县");

p.setBirthday(new Date());

try {

session.insert("com.rl.mapper.PersonMapper.insert", p);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

/**

* 修改,将id是3的记录改成

*/

@Test

public void update(){

SqlSession session = sessionFactory.openSession();

Person p = new Person();

p.setPersonId(3);

p.setName("陆虞候");

p.setGender("0");

p.setPersonAddr("阳谷县");

p.setBirthday(new Date());

try {

session.update("com.rl.mapper.PersonMapper.update", p);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

/**

* 删除

*/

@Test

public void delete(){

SqlSession session = sessionFactory.openSession();

try {

session.delete("com.rl.mapper.PersonMapper.delete", 4);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

}

MybatisTest2.java


package com.rl.test;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

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.Before;

import org.junit.Test;

import com.rl.model1.Person;

/**

* mybatis的动态sql

*/

public class MybatisTest2 {

SqlSessionFactory sessionFactory;

@Before

public void setUp() throws Exception {

InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");

sessionFactory = new SqlSessionFactoryBuilder().build(in);

}

@Test

public void selectPersonByCondition() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Map<String, Object> map = new HashMap<String, Object>();

map.put("name", "安");

map.put("gender", "0");

//map.put("personAddr", "东京");

//map.put("birthday", new Date());

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

List<Person> pList  = session.selectList("com.rl.mapper.PersonMapper.selectPersonByCondition", map);

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

@Test

public void dynamicUpdate(){

SqlSession session = sessionFactory.openSession();

Person p = new Person();

p.setPersonId(3);

p.setName("陆虞候");

p.setGender("0");

//p.setPersonAddr("阳谷县");

//p.setBirthday(new Date());

try {

session.update("com.rl.mapper.PersonMapper.dynamicUpdate", p);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

/**

* foreach的用法

*/

@Test

public void selectPersonByIn() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Map<String, Object> map = new HashMap<String, Object>();

/*List list = new ArrayList();

list.add(1);

list.add(2);

list.add(3);*/

String [] list = {"1","2","3"};

map.put("ids", list);

List<Person> pList  = session.selectList("com.rl.mapper.PersonMapper.selectPersonByIn", map);

for(Person p : pList){

System.out.println(p);

}

} finally{

session.close();

}

}

/**

* 批量插入

*/

@Test

public void insertBatch(){

SqlSession session = sessionFactory.openSession();

Map<String,Object> map = new HashMap<String,Object>();

List<Person> pList= new ArrayList<Person>();

try {

for(int i = 0; i < 1000009; i++){

Person p = new Person();

p.setName("武松"+i);

p.setGender("0");

p.setPersonAddr("阳谷县");

p.setBirthday(new Date());

pList.add(p);

if(i%100 == 0){

map.put("pList", pList);

session.insert("com.rl.mapper.PersonMapper.insertBatch", map);

pList.clear();

}

}

map.put("pList", pList);

session.insert("com.rl.mapper.PersonMapper.insertBatch", map);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

@Test

public void deleteBatch(){

SqlSession session = sessionFactory.openSession();

Map<String,Object> map = new HashMap<String,Object>();

List<Integer> ids= new ArrayList<Integer>();

try {

for(int i = 106; i < 1000115; i++){

ids.add(i);

if(i%100 == 0){

map.put("ids", ids);

session.delete("com.rl.mapper.PersonMapper.deleteBatch", map);

ids.clear();

}

}

map.put("ids", ids);

session.insert("com.rl.mapper.PersonMapper.deleteBatch", map);

//库表的变更都需要提交

session.commit();

} catch (Exception e) {

e.printStackTrace();

session.rollback();

}finally{

session.close();

}

}

}

MybatisTest3.java的内容如下:


package com.rl.test;

import java.io.InputStream;

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.Before;

import org.junit.Test;

import com.rl.model1.Orders;

import com.rl.model1.Person;

import com.rl.model1.Role;

/**

*
mybatis的关联查询

*/

public
class MybatisTest3 {

SqlSessionFactory sessionFactory;

@Before

public
void setUp()
throws Exception {

InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");

sessionFactory =
new SqlSessionFactoryBuilder().build(in);

}

/**

* 一对多关联查询

*/

@Test

public
void selectPersonAndOrderByPId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

Person person  = session.selectOne("com.rl.mapper.PersonMapper.selectPersonAndOrderByPId",
1);

System.out.println(person);

} finally{

session.close();

}

}

/**

* 查询Person下的所有订单和订单下的明细

*/

@Test

public
void selectPersonOrderAndDetailByPId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

Person person  = session.selectOne("com.rl.mapper.PersonMapper.selectPersonOrderAndDetailByPId",
1);

System.out.println(person);

} finally{

session.close();

}

}

/**

* 多对多查询从Person端出发

*/

@Test

public
void selectPersonAndRoleByPId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

Person person  = session.selectOne("com.rl.mapper.PersonMapper.selectPersonAndRoleByPId",
1);

System.out.println(person);

} finally{

session.close();

}

}

/**

* 多对多查询从角色端来看

*/

@Test

public
void selectRoleAndPersonByRId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

//第一个参数:指定要执行的sql语法是namespace.sql的id,第二个参数sql要接收的参数

Role role  = session.selectOne("com.rl.mapper.RoleMapper.selectRoleAndPersonByRId",
1);

System.out.println(role);

} finally{

session.close();

}

}

/**

* 多对一的关联查询

*这里的mapper配置文件在后续的博文中定义。

*/

@Test

public
void selectPersonByOrderId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Orders order  = session.selectOne("com.rl.mapper.OrdersMapper.selectPersonByOrderId",
1);

System.out.println(order);

} finally{

session.close();

}

}

/**

* 多对一和一对多混合查询

*
这里的mapper配置文件在后续的博文中定义。

*/

@Test

public
void selectPersonAndDetailByOrderId() {

//创建session对象

SqlSession session = sessionFactory.openSession();

try {

Orders order  = session.selectOne("com.rl.mapper.OrdersMapper.selectPersonAndDetailByOrderId",
1);

System.out.println(order);

} finally{

session.close();

}

}

}

时间: 01-20

03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma的相关文章

mapper文件提示:No data sources are configured to run this sql

mapper文件发出黄色警告. 输入数据库用户名和密码等等. 自动同步ok 就会发现代码变绿了,ok 原文地址:https://www.cnblogs.com/wangyuehan/p/9892288.html

文件编码检测.测试代码

ZC:这个是在 G转SVG的C++项目中要用到这个功能的,然后逐步查资料查到 jchardet的,相关的文章为"文件编码检测.ZC一些资料(包含java的) - CppSkill - 博客园.html(https://www.cnblogs.com/cppskill/p/9906599.html)" ZC:相关文件位于:"..\ZC_IDE\Java_3rd\ZC_文件编码自动检测\jchardet-1.1.zip",jar 位于 "...\ZC_IDE\

文件上传时如何携带参数和接收返回值(亲测有效)

1.页面 2.js 3.java后台接收 亲测有效 原文地址:https://www.cnblogs.com/ysyy/p/12174535.html

实现按条件查询

第一步: 定义接口: public interface ICommonDao<T> { List<T> findCollectionByConditionNoPage(String codition,Object[] params, Map<String, String> orderby); } 第二步: 实现接口的类: Class entityClass = TUtils.getTClass(this.getClass()); public class TUtils

apklug插件托管服务开发教程之按条件查询云端插件-01

以一个简单的输出程序为例: #include<iostream> #include<cstring> #include<cstdio> using namespace std; class point { public: int a,b,c; }; point p; void fuc() { for(int i=1;i<=10;++i) { ++p.a; ++p.b; ++p.c; cout<<p.a<<' '<<p.b<

MyBatis关联查询、多条件查询

1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Classes { 9     //定义实体类的属性,与class表中的字段对应 10     private int id;            //id===>c_id 11     private String name;    //name===>c_name 13     /** 14      * class表中有一个tea

sql server查询可编程对象定义的方式对比以及整合

本文目录列表: 1.sql server查看可编程对象定义的方式对比 2.整合实现所有可编程对象定义的查看功能的存储dbo.usp_helptext2 3.dbo.helptext2的选择性测试 4.总结语 5.参考清单列表 1.sql server查看可编程对象定义的方式对比   上一篇博文重构sql server的sys.helptext存储中写了sys.helptext的限制和输出格式每行自带char(13)和char(10)这两个字符.为了将可编程对象定义查询方式研究透彻,以下表格列出了

thinkphp基础学习简单条件查询

       $user = new Model("User");//        字符串条件查询        var_dump($user->where('id=42 AND username="bnbbs"')->select());        $map['id']=1;        $map['username']="bnbbs";        $map['email']="[email protected

thinkphp条件查询和模糊查询的一些方法

1 #文章管理 2 public function adminArticle(){ 3 $adminArticle=M("article"); 4 $arr_seach=$this->seach($adminArticle,10,"now desc","");//参数格式seach($db,$num,$order,$where) 5 ##按最早排序 6 if([email protected]empty($_POST['time'])){