未成年人微信支付密码忘了,现在零钱又微信零钱大于5000,不能注销实名制,该怎么办?

mybatis一对多映射
这次我们给Perosn拥有了Car,Car的属性为id,name(简单起见),一个Person可以有多个Car,
在插入Person时,把其拥有的Car插入数据库表t_car
mybatis : CarMapper.xml配置如下
INSERT INTO t_car (name, owner_id)
(#{car.name}, #{personId}),
(#{car.name}, #{personId})
cars为通过传参过来的Map的一个key,通过中的简单判断来一次插入多条记录
PersonMapper.xml
select="com.leecode.mybatis.Car.selectWithId"&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。原始目标:
配置好mapper,使得可以在实体中表示表之间的联系(一个表外键,用另一个实体表示)
深读了mybatis 官方的文章,最后总结出一最重要的的一条,一定要好好利用官方 的autoMapping 特性,否则就得一条条写映射关系了。当然对于实体的嵌套填充, 我只做一层,再往下走就需要在程序逻辑上做一些处理
这里配置的逻辑只与表在逻辑上的连接相关,是否在数据中实现关系无关。
一、犯过的错误
& & & & 一开始我把所有相关的表的列都放进来,导致有错误不能排除,所以搞了一天加一晚上。后面我只要了几个关键的,这样语句有错误也好调试
二、正确的认识
& & 为了有效利用automaping 特性,在取别名时除前缀外,后面的名称我们将采取与实体属性一样的名称,这样,我们在resultmap中就无需再一一对上属性
& & &resultMap&id=&psProjectPushResultMap& & & & & type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsProjectPush&&&autoMapping&=&true&&
&&&id&property=&id&&
column=&id&/&
&/resultMap&
& & 这里的column我们只需要考虑不包括前缀的
3.映射方式主要有两种
& & &第一种:直接取别名,用resultType返回结果,这种方式只能针对简单的,且没有List&Entity&之类的 & & 直接取名时,对于entity属性别名一般取属性.id(
a.customer_id as customer.id),这样mybatis会将这个值送给对像的ID字段.
& & 第二种:用resultMap返回结果,当有List&Entity&这种Collection时必须用这一种方式,以下为相关解释
& &association&
property=&customer&&&column=&
customerid&&&resultMap=&psCustomerResultMap&&columnPrefix=&customer_&/&
& & property 为实体中的属性值,customerid为sql语句中的别名(一般是用一关联另一实体,在使用association这种方式下,这个别名保留与表格相同就好
4关于嵌套执行sql还是一条SQL连接多表
& & 嵌套执行:官方演示了此方式,也方便理解,但由于效率太差不推荐
& & 一条大SQL(JOIN):此方式是官方 推荐方式,映射时官方 会自自己移除重复记录,此也为推荐方式。
5.关联的主表字段取别名时不需要加前缀。
6.连接时注意连接方式,一般是以左连接为主
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
三、注意事项
官方举例在别名时推荐使用的是下划线,
1. 但在以下的别名方式下,.也是可以的。
&&sql&id=&psCustomerColumns&&
&&&&&&&&c.id&as&&customer_id&,
&&&&&&&&c.code&as&&customer_code&,
&&&&&&&&c.name&as&&customer_name&
&&&&&/sql&
2.此类别名方式下
&sql&id=&psProjectPushColumns&&
&&&&&&&&push.id&as&&push.id&,
&&&&&&&&push.projectid&AS&&push.project.id&,
&&&&&&&&push.userid&AS&&push.user.id&,
&&&&&&&&push.is_readed&AS&&push.isReaded&
&&&&&/sql&
也是可以的
如果下划线以外方式不能正确工作,就改用官方的下划线前缀。
三、其实应该注意的点
现在来看看这个mapper的映射配置如何编写,注意示例中的以下几点: 1、constructor& 实体的构造方法 2、autoMapping& 自动属性映射 3、collection& 集合属性的映射 4、association& 关联属性的映射
5、mapUnderscoreToCamelCase 是否开启自动驼峰命名规则,全局配置.
实例一,使用下划线:
PsProjectDao.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.thinkgem.jeesite.modules.projectschedule.dao.PsProjectDao&&
&!-- &typeAlias type=&com.someapp.model.User& alias=&User&/& --&
&!-- begin result map area --&
&resultMap id=&psProjectResultMap& type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsProject&
autoMapping =&true&&
&id property=&id& column=&id& /&
&result property=&title& column=&blog_title&/&--&
&association property=&customer&
column=&customerid&
resultMap=&psCustomerResultMap& columnPrefix=&customer_&/&
&association property=&department& column=&departmentid& resultMap=&departmentResultMap& columnPrefix=&department_&/&
&association property=&projectManager& column=&projectManagerid& resultMap=&userResultMap& columnPrefix=&projectManager_&/&
&association property=&marketer& column=&marketerid& resultMap=&userResultMap& columnPrefix=&marketer_&/&
&collection
property=&push&
column=&push& ofType=&PsProjectPush& resultMap=&psProjectPushResultMap&
columnPrefix=&push_&/&
&/resultMap&
&resultMap id=&userResultMap& type=&com.thinkgem.jeesite.modules.sys.entity.User&
autoMapping =&true&&
&id property=&id& column=&id&/&
&/resultMap&
&resultMap id=&departmentResultMap& type=&com.thinkgem.jeesite.modules.sys.entity.Office& autoMapping =&true& &
&id property=&id& column=&id&/&
&/resultMap&
&resultMap id=&psCustomerResultMap& type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsCustomer&
autoMapping =&true&&
&id property=&id& column=&id&/&
&/resultMap&
&resultMap id=&psProjectPushResultMap& type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsProjectPush&
autoMapping =&true&&
&id property=&id& column=&id&/&
&/resultMap&
&!-- end resultmap area --&
&!-- begin sql columns area --&
&sql id=&psProjectColumns&&
a.id AS &id&,
a.code AS &code&,
a.name AS &name&,
a.type AS &type&,
a.importance_degree AS &importanceDegree&,
a.tech_state AS &techState&,
a.customerid AS &customerid&,
a.departmentid AS &departmentid&,
a.project_managerid AS &projectManagerid&,
a.plan_starttime AS &planStarttime&,
a.plan_endtime AS &planEndtime&,
a.state AS &state&,
a.act_starttime AS &actStarttime&,
a.act_endtime AS &actEndtime&,
a.sending_time AS &sendingTime&,
a.old_plan_starttime AS &oldPlanStarttime&,
a.old_plan_endtime AS &oldPlanEndtime&,
a.delivery_time AS &deliveryTime&,
a.complete_status AS &completeStatus&,
a.marketerid AS &marketerid&,
a.create_by AS &createBy.id&,
a.create_date AS &createDate&,
a.update_by AS &updateBy.id&,
a.update_date AS &updateDate&,
a.remarks AS &remarks&,
a.del_flag AS &delFlag&
&sql id=&departmentColumns&&
d.id as &department_id&,
d.name as &department_name&
&sql id=&projectManagerColumns&&
pm.id as &projectManager_id&,
pm.name as &projectManager_name&
&sql id=&marketerColumns&&
m.id as &marketer_id&,
m.name as &marketer_name&
&sql id=&psCustomerColumns&&
c.id as &customer_id&,
c.code as &customer_code&,
c.name as &customer_name&
&sql id=&psProjectPushColumns&&
ps.id as &push_id&,
ps.projectid AS &push_project.id&,
ps.userid AS &push_user.id&,
ps.is_readed AS &push_isReaded&
&!-- end sql columns area --&
&sql id=&psProjectJoins&&
LEFT JOIN ps_customer c ON c.id = a.customerid
LEFT JOIN sys_office
d ON d.id = a.departmentid
LEFT JOIN sys_user pm ON pm.id = a.project_managerid
LEFT JOIN sys_user m ON m.id = a.marketerid
LEFT JOIN ps_project_push ps ON ps.projectid=a.id
&select id=&findAllList& resultMap=&psProjectResultMap&&
&include refid=&psProjectColumns&/&,
&include refid=&departmentColumns&/&,
&include refid=&projectManagerColumns&/&,
&include refid=&marketerColumns&/&,
&include refid=&psCustomerColumns&/&,
&include refid=&psProjectPushColumns&/&
FROM ps_project a
&include refid=&psProjectJoins&/&
a.del_flag = #{DEL_FLAG_NORMAL}
&when test=&page !=null and page.orderBy != null and page.orderBy != ''&&
ORDER BY ${page.orderBy}
&otherwise&
ORDER BY a.update_date DESC
&/otherwise&
&insert id=&insert&&
INSERT INTO ps_project(
importance_degree,
tech_state,
customerid,
departmentid,
managerid,
plan_starttime,
plan_endtime,
act_starttime,
act_endtime,
sending_time,
old_plan_starttime,
old_plan_endtime,
delivery_time,
complete_status,
marketerid,
create_by,
create_date,
update_by,
update_date,
) VALUES (
#{importanceDegree},
#{techState},
#{customer.id},
#{department.id},
#{projectManager.id},
#{planStarttime},
#{planEndtime},
#{actStarttime},
#{actEndtime},
#{sendingTime},
#{oldPlanStarttime},
#{oldPlanEndtime},
#{deliveryTime},
#{completeStatus},
#{marketer.id},
#{createBy.id},
#{createDate},
#{updateBy.id},
#{updateDate},
#{remarks},
#{delFlag}
&update id=&update&&
UPDATE ps_project SET
code = #{code},
name = #{name},
type = #{type},
importance_degree = #{importanceDegree},
tech_state = #{techState},
customerid = #{customer.id},
departmentid = #{department.id},
managerid = #{projectManager.id},
plan_starttime = #{planStarttime},
plan_endtime = #{planEndtime},
state = #{state},
act_starttime = #{actStarttime},
act_endtime = #{actEndtime},
sending_time = #{sendingTime},
old_plan_starttime = #{oldPlanStarttime},
old_plan_endtime = #{oldPlanEndtime},
delivery_time = #{deliveryTime},
complete_status = #{completeStatus},
marketerid = #{marketer.id},
update_by = #{updateBy.id},
update_date = #{updateDate},
remarks = #{remarks}
WHERE id = #{id}
&update id=&delete&&
UPDATE ps_project SET
del_flag = #{DEL_FLAG_DELETE}
WHERE id = #{id}
实例二:使用.做为前缀
&?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.thinkgem.jeesite.modules.projectschedule.dao.PsProjectDao&&
&!-- &typeAlias type=&com.someapp.model.User& alias=&User&/& --&
&!-- begin result map area --&
&resultMap id=&psProjectResultMap&
type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsProject&
autoMapping=&true&&
&id property=&id& column=&id& /&
&!-- &result property=&title& column=&blog_title&/& --&
&association property=&customer& column=&customerid&
resultMap=&psCustomerResultMap& columnPrefix=&customer.& /&
&association property=&department& column=&departmentid&
resultMap=&departmentResultMap& columnPrefix=&department.& /&
&association property=&projectManager& column=&projectManagerid&
resultMap=&userResultMap& columnPrefix=&projectManager.& /&
&association property=&marketer& column=&marketerid&
resultMap=&userResultMap& columnPrefix=&marketer.& /&
&collection property=&push& column=&push& ofType=&PsProjectPush&
resultMap=&psProjectPushResultMap& columnPrefix=&push.& /&
&/resultMap&
&resultMap id=&userResultMap& type=&com.thinkgem.jeesite.modules.sys.entity.User&
autoMapping=&true&&
&id property=&id& column=&id& /&
&/resultMap&
&resultMap id=&departmentResultMap&
type=&com.thinkgem.jeesite.modules.sys.entity.Office& autoMapping=&true&&
&id property=&id& column=&id& /&
&/resultMap&
&resultMap id=&psCustomerResultMap&
type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsCustomer&
autoMapping=&true&&
&id property=&id& column=&id& /&
&/resultMap&
&resultMap id=&psProjectPushResultMap&
type=&com.thinkgem.jeesite.modules.projectschedule.entity.PsProjectPush&
autoMapping=&true&&
&id property=&id& column=&id& /&
&/resultMap&
&!-- end resultmap area --&
&!-- begin sql columns area --&
&sql id=&psProjectColumns&&
a.id AS &id&,
a.code AS &code&,
a.name AS &name&,
a.importance_degree AS &importanceDegree&,
a.tech_state AS
&techState&,
a.customerid AS &customerid&,
a.departmentid AS
&departmentid&,
a.project_managerid AS &projectManagerid&,
a.plan_starttime AS &planStarttime&,
a.plan_endtime AS &planEndtime&,
a.state AS &state&,
a.act_starttime AS &actStarttime&,
a.act_endtime AS
&actEndtime&,
a.sending_time AS &sendingTime&,
a.old_plan_starttime AS
&oldPlanStarttime&,
a.old_plan_endtime AS &oldPlanEndtime&,
a.delivery_time AS &deliveryTime&,
a.complete_status AS
&completeStatus&,
a.marketerid AS &marketerid&,
a.create_by AS
&createBy.id&,
a.create_date AS &createDate.id&,
a.update_by AS
&updateBy.id&,
a.update_date AS &updateDate.id&,
a.remarks AS &remarks&,
a.del_flag AS &delFlag&
&sql id=&departmentColumns&&
department.id as &department.id&,
department.name as &department.name&
&sql id=&projectManagerColumns&&
projectManager.id as &projectManager.id&,
projectManager.name as &projectManager.name&
&sql id=&marketerColumns&&
marketer.id as &marketer.id&,
marketer.name as &marketer.name&
&sql id=&psCustomerColumns&&
customer.id as &customer.id&,
customer.code as
&customer.code&,
customer.name as &customer.name&
&sql id=&psProjectPushColumns&&
push.id as &push.id&,
push.projectid AS &push.project.id&,
push.userid AS &push.user.id&,
push.is_readed AS &push.isReaded&
&!-- end sql columns area --&
&sql id=&psProjectJoins&&
LEFT JOIN ps_customer customer ON customer.id =
a.customerid
LEFT JOIN sys_office department ON department.id = a.departmentid
LEFT JOIN sys_user projectManager ON projectManager.id =
a.project_managerid
LEFT JOIN sys_user marketer ON marketer.id = a.marketerid
LEFT JOIN ps_project_push push ON push.projectid=a.id
&select id=&findAllList& resultMap=&psProjectResultMap&&
&include refid=&psProjectColumns& /&
&include refid=&departmentColumns& /&
&include refid=&projectManagerColumns& /&
&include refid=&marketerColumns& /&
&include refid=&psCustomerColumns& /&
&include refid=&psProjectPushColumns& /&
FROM ps_project a
&include refid=&psProjectJoins& /&
a.del_flag = #{DEL_FLAG_NORMAL}
&when test=&page !=null and page.orderBy != null and page.orderBy != ''&&
ORDER BY ${page.orderBy}
&otherwise&
ORDER BY a.update_date DESC
&/otherwise&
&insert id=&insert&&
INSERT INTO ps_project(
importance_degree,
tech_state,
customerid,
departmentid,
managerid,
plan_starttime,
plan_endtime,
act_starttime,
act_endtime,
sending_time,
old_plan_starttime,
old_plan_endtime,
delivery_time,
complete_status,
marketerid,
create_by,
create_date,
update_by,
update_date,
) VALUES (
#{importanceDegree},
#{techState},
#{customer.id},
#{department.id},
#{projectManager.id},
#{planStarttime},
#{planEndtime},
#{actStarttime},
#{actEndtime},
#{sendingTime},
#{oldPlanStarttime},
#{oldPlanEndtime},
#{deliveryTime},
#{completeStatus},
#{marketer.id},
#{createBy.id},
#{createDate},
#{updateBy.id},
#{updateDate},
#{remarks},
#{delFlag}
&update id=&update&&
UPDATE ps_project SET
code = #{code},
name = #{name},
type = #{type},
importance_degree = #{importanceDegree},
tech_state = #{techState},
customerid = #{customer.id},
departmentid = #{department.id},
managerid = #{projectManager.id},
plan_starttime = #{planStarttime},
plan_endtime = #{planEndtime},
state = #{state},
act_starttime = #{actStarttime},
act_endtime = #{actEndtime},
sending_time = #{sendingTime},
old_plan_starttime = #{oldPlanStarttime},
old_plan_endtime = #{oldPlanEndtime},
delivery_time = #{deliveryTime},
complete_status = #{completeStatus},
marketerid = #{marketer.id},
update_by = #{updateBy.id},
update_date = #{updateDate},
remarks = #{remarks}
WHERE id = #{id}
&update id=&delete&&
UPDATE ps_project SET
del_flag = #{DEL_FLAG_DELETE}
WHERE id = #{id}
实体文件(PsProject.java)
* 项目管理Entity
* @author xiaohelong
* @version
public class PsProject extends DataEntity&PsProject& {
private static final long serialVersionUID = 1L;
// 项目编号
// 项目名称
// 项目类型(科研项目,还是交付)
private String importanceD
// 重要程度(一般,重要,紧急)
private String techS
// 技术状态(完全沿用,设计更改,全新设计)
private PsC
// 客户单位
// 所属部门
private User projectM
// 项目经理
private Date planS
// 计划开始时间
private Date planE
// 计划结束时间
// 项目当前状态(等待开始,正常进行,交付延期,节点延期,项目暂停,项目终止,项目结束)
private Date actS
// 实际开始时间
private Date actE
// 实际结束时间
private Date sendingT
// 项目下发时间
private Date oldPlanS
// 计划开始时间(只读字段,初始化后不更改)
private Date oldPlanE
// 计划结束时间(只读字段,初始化后不更改)
private Date deliveryT
// 交付时间
private String completeS
// 完成情况(正常完成,延期完成,未完成)
// 市场人员ID
private List&PsProjectPush& //相关领导数据表中并没有,放在这里便于表单操作,因为该字段数据送到另外一个表中了。
public PsProject() {
public PsProject(String id){
super(id);
public String getCode() {
public void setCode(String code) {
this.code =
public String getName() {
public void setName(String name) {
this.name =
public String getType() {
public void setType(String type) {
this.type =
public String getImportanceDegree() {
return importanceD
public void setImportanceDegree(String importanceDegree) {
this.importanceDegree = importanceD
public String getTechState() {
return techS
public void setTechState(String techState) {
this.techState = techS
public PsCustomer getCustomer() {
public void setCustomer(PsCustomer customer) {
this.customer =
public Office getDepartment() {
public void setDepartment(Office department) {
this.department =
public User getProjectManager() {
return projectM
public void setProjectManager(User projectManager) {
this.projectManager = projectM
public Date getPlanStarttime() {
return planS
public void setPlanStarttime(Date planStarttime) {
this.planStarttime = planS
public Date getPlanEndtime() {
return planE
public void setPlanEndtime(Date planEndtime) {
this.planEndtime = planE
public String getState() {
public void setState(String state) {
this.state =
public Date getActStarttime() {
return actS
public void setActStarttime(Date actStarttime) {
this.actStarttime = actS
public Date getActEndtime() {
return actE
public void setActEndtime(Date actEndtime) {
this.actEndtime = actE
public Date getSendingTime() {
return sendingT
public void setSendingTime(Date sendingTime) {
this.sendingTime = sendingT
public Date getOldPlanStarttime() {
return oldPlanS
public void setOldPlanStarttime(Date oldPlanStarttime) {
this.oldPlanStarttime = oldPlanS
public Date getOldPlanEndtime() {
return oldPlanE
public void setOldPlanEndtime(Date oldPlanEndtime) {
this.oldPlanEndtime = oldPlanE
public Date getDeliveryTime() {
return deliveryT
public void setDeliveryTime(Date deliveryTime) {
this.deliveryTime = deliveryT
public String getCompleteStatus() {
return completeS
public void setCompleteStatus(String completeStatus) {
pleteStatus = completeS
public User getMarketer() {
public void setMarketer(User marketer) {
this.marketer =
public List&PsProjectPush& getPush() {
public void setPush(List&PsProjectPush& push) {
this.push =
另附直接用别名简单映射方式(无LIST collection)--JeeSite默认的方式,没有继续研究其List是如何实现的,因其并没有在XML中体现出来,所以个人觉得很可能是在JAVA SERVICE程进行了体现。
&?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.thinkgem.jeesite.modules.sys.dao.OfficeDao&&
&sql id=&officeColumns&&
a.parent_id AS &parent.id&,
a.parent_ids,
a.area_id AS &area.id&,
a.address,
a.zip_code,
a.remarks,
a.create_by AS &createBy.id&,
a.create_date,
a.update_by AS &updateBy.id&,
a.update_date,
a.del_flag,
a.useable AS useable,
a.primary_person AS &primaryPerson.id&,
a.deputy_person AS &deputyPerson.id&,
p.name AS &parent.name&,
ar.name AS &area.name&,
ar.parent_ids AS &area.parentIds&,
pp.name AS &primaryPerson.name&,
dp.name AS &deputyPerson.name&
&sql id=&officeJoins&&
LEFT JOIN sys_office p ON p.id = a.parent_id
LEFT JOIN sys_area ar ON ar.id = a.area_id
LEFT JOIN SYS_USER pp ON pp.id = a.primary_person
LEFT JOIN SYS_USER dp ON dp.id = a.deputy_person
&select id=&findAllList& resultType=&Office&&
&include refid=&officeColumns&/&
FROM sys_office a
&include refid=&officeJoins&/&
WHERE a.del_flag = #{DEL_FLAG_NORMAL}
ORDER BY a.code
参考资料:
1. http://www.mybatis.org/mybatis-3/sqlmap-xml.html#Auto-mapping
2.http://leeyee.github.io/blog//mybatis-association-autoMapping/学习过Hibernate框架的伙伴们很容易就能简单的配置各种映射关系(Hibernate框架的映射关系在我的blogs中也有详细的讲解),但是在Mybatis框架中我们又如何去实现
一对多的关系映射呢?&其实很简单
首先我们照常先准备前期的环境(具体解释请 &参考初识Mybatis进行增、删、改、查 blogs )这里我就直接上代码了
主配置文件:Configuration.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&
&environments default="development"&
&environment id="development"&
&transactionManager type="JDBC"&
&property name="" value=""/&
&/transactionManager&
&dataSource type="UNPOOLED"&
&property name="driver" value="oracle.jdbc.OracleDriver"/&
&property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/&
&property name="username" value="practice"/&
&property name="password" value="123"/&
&/dataSource&
&/environment&
&/environments&
&mapper resource="config/Student.xml"/&
&mapper resource="config/Grade.xml"/&
&/mappers&
&/configuration&
背景:学生和班级是一个典型的一对多的关系,一个班级可以对应着多个学生,所以我们随即创建了学生对象和班级对象
学生类:Student
public class Student {
//学生编号
//学生名称
//学生性别
public Student() {
public Student(String sname, String sex) {
this.sname =
this.sex =
public Integer getSid() {
public void setSid(Integer sid) {
this.sid =
public String getSname() {
public void setSname(String sname) {
this.sname =
public String getSex() {
public void setSex(String sex) {
this.sex =
班级类:Grade
import java.util.HashS
import java.util.S
public class Grade {
//班级编号
//班级名称
//班级描述
//班级下的学生信息
private Set&Student& stus=new HashSet&Student&();
public Set&Student& getStus() {
public void setStus(Set&Student& stus) {
this.stus =
public Grade() {
public Grade(Integer gid, String gname, String gdesc) {
this.gid =
this.gname =
this.gdesc =
public Integer getGid() {
public void setGid(Integer gid) {
this.gid =
public String getGname() {
public void setGname(String gname) {
this.gname =
public String getGdesc() {
public void setGdesc(String gdesc) {
this.gdesc =
实体类准备完了的话,我们就可以开始看配置文件了,也是最关键的一部分
首先讲简单点的学生实体类对应的配置文件
Student.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="Student"&
&resultMap type="entity.Student" id="StudentResult"&
&id column="sid" jdbcType="INTEGER" property="sid"/&
&result column="sname" jdbcType="VARCHAR" property="sname"/&
&result column="sex" jdbcType="VARCHAR" property="sex"/&
&/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"&
&mapper namespace="Grade"&
&resultMap type="entity.Grade" id="GradeResult"&
&id column="gid" jdbcType="INTEGER" property="gid"/&
&result column="gname" jdbcType="VARCHAR" property="gname"/&
&result column="gdesc" jdbcType="VARCHAR" property="gdesc"/&
&!-- 一对多关系 --&
&collection property="stus" resultMap="Student.StudentResult"&&/collection&
&/resultMap&
&!-- 查询所有信息 --&
&select id="selectAllInfo" resultMap="GradeResult"&
&!-- select sid,sname,sex,g.gid,gname,gdesc from Student s,Grade g where s.gid=g.gid --&
select sid,sname,sex,g.gid,gname,gdesc from Student s left join Grade g on s.gid=g.gid
以上就是对配置文件的解释了
接下来我们就可以进行一道测试了
* 1.1 查询所有的班级和班级下的所有学生(一对多)
public void selectAllStu() throws Exception{
//通过配置文件获取到数据库连接信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//通过配置信息构建一个SessionFactory工厂
SqlSessionFactory sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader);
//通过SessionFaction打开一个回话通道
SqlSession session = sqlsessionfactory.openSession();
/*SqlSession session =MybatisUtil.getSession();*/
//调用配置文件中的sql语句
List&Grade& list = session.selectList("Grade.selectAllInfo");
//遍历查询出来的结果
for (Grade grade : list) {
System.out.println("班级:"+grade.getGname());
for (Student stu : grade.getStus()) {
System.out.println("学生:"+stu.getSname());
session.close();
执行后,查询出来的结果是
以上是第一种一对多关系映射的方式,下面是第二种一对多映射的方法,其他的所有步骤和上面的都是一样的只有相对应的配置文件不同,所以我就只贴小配置了
&?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="Grade"&
&resultMap type="entity.Grade" id="GradeResult"&
&id column="gid" jdbcType="INTEGER" property="gid"/&
&result column="gname" jdbcType="VARCHAR" property="gname"/&
&result column="gdesc" jdbcType="VARCHAR" property="gdesc"/&
&!-- 一对多关系 --&
&!-- &collection property="stus" resultMap="Student.StudentResult"&&/collection&
&collection property="stus" javaType="entity.Student"&
&id property="sid" column="sid"/&
&result property="sname" column="sname"/&
&result property="sex" column="sex"/&
&/collection&
&/resultMap&
&!-- 查询所有信息 --&
&select id="selectAllInfo" resultMap="GradeResult"&
&!-- select sid,sname,sex,g.gid,gname,gdesc from Student s,Grade g where s.gid=g.gid --&
select sid,sname,sex,g.gid,gname,gdesc from Student s left join Grade g on s.gid=g.gid
&!-- 新增班级并同时新增班级下的学生 --&
&!--useGeneratedKeys=true 表明采用主键生成策略
keyProperty="gid"
表明将生成的主键添加到parameterType类中的那个属性值中去
&!-- &insert id="" useGeneratedKeys="true" keyProperty="gid" parameterType="entity.Grade"&
&/insert& --&
接下来就可以在多的一方配置一的关联关系了
Student.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="Student"&
&resultMap type="entity.Student" id="StudentResult"&
&id column="sid" jdbcType="INTEGER" property="sid"/&
&result column="sname" jdbcType="VARCHAR" property="sname"/&
&result column="sex" jdbcType="VARCHAR" property="sex"/&
&!-- 多对一 --&
&association property="grade" resultMap="Grade.GradeResult"&&/association& --&
&association property="grade" javaType="entity.Grade"&
&id property="gid" column="gid"/&
&result property="gname" column="gname"/&
&result property="gdesc" column="gdesc"/&
&/association&
&/resultMap&
&!-- 使用别名 --&
&sql id="cloums"&
s.sid,s.sname,s.sex ,g.gid,g.gname,g.gdesc
&!-- 多对一查询学生的班级 --&
&select id="selectGradeByStu" resultMap="StudentResult"&
select &include refid="cloums"/& from Student s ,Grade g where s.gid=g.gid
&!-- 简单查询所有信息 --&
&select id="selectAllStu"
resultMap="StudentResult"&
select sid,sname,sex,gid from Student
&!--动态拼接Sql
&select id="selectAllStuByWhere" parameterType="entity.Student"
resultMap="StudentResult"&
select sid,sname,sex,gid from Student where 1=1
&if test="sname!=null and !&&.equals(sname.trim())"&
&!-- and sname like '%'|| #{sname}|| '%' --& &!-- 模糊查询 --&
and sname like '%${sname}%'&!-- 模糊查询 --&
&!-- and sname = #{sname} --&
&!-- 新增学生信息 --&
&insert id="InsertStuInfo" parameterType="entity.Student" &
insert into Student values(SEQ_NUM.Nextval,#{sname},#{sex},1)
&!-- 删除学生信息 --&
&insert id="DeleteStuBySid" parameterType="int"&
delete from Student where sid=#{sid}
&!--或者是
delete from Student where sid=#{_parameter} --&
&!-- 根据SID修改学生信息 --&
&update id="UpdateStuBySid" parameterType="entity.Student" &
update Student set sname=#{sname},sex=#{sex} where sid=#{sid}
这样就已经完成了Mybatis框架中的简单的双向一对多的配置了,怎么样,是不是没有想象中的难呢?希望对大家有点帮助吧!
阅读(...) 评论()

我要回帖

更多关于 取消微信零钱支付密码 的文章

 

随机推荐