- 浏览: 162510 次
- 性别:
- 来自: 北京
最新评论
-
BlueAeri:
好像旧了,有些参数没有,比如 --cups
VBoxManage命令详解 -
jiamb:
非常感谢!
学习ibatis的一些资料 -
memory_in_08:
正急着找ibatis的此资料呢,3k you..
学习ibatis的一些资料 -
小猪笨笨:
多谢···
学习ibatis的一些资料 -
mingxiao2010:
谢谢你的分享!!
学习ibatis的一些资料
iBatis2学习笔记:多对多映射(双向)
环境:
MySQL5.51b
iBatis 2.3
MySQL5.51b
iBatis 2.3
Spring 2.5.4
这个多对多不容易,费了好大劲才实现。
一、模型
用户(User)和角色(Role)之间是多对多的关系。
二、SQL
/*==============================================================*/
/* Table: role */
/*==============================================================*/
create table role
(
id bigint not null auto_increment,
rolename varchar(24),
descp varchar(240),
primary key (id)
);
alter table role comment '角色
';
/*==============================================================*/
/* Table: tlink */
/*==============================================================*/
create table tlink
(
userId bigint not null,
roleId bigint not null
);
alter table tlink comment '连接表';
/*==============================================================*/
/* Table: user */
/*==============================================================*/
create table user
(
id bigint not null auto_increment,
username varchar(24),
remark varchar(240),
primary key (id)
);
alter table user comment '用户';
alter table tlink add constraint FK_r foreign key (roleId)
references role (id) on delete restrict on update restrict;
alter table tlink add constraint FK_u foreign key (userId)
references user (id) on delete restrict on update restrict;
/* Table: role */
/*==============================================================*/
create table role
(
id bigint not null auto_increment,
rolename varchar(24),
descp varchar(240),
primary key (id)
);
alter table role comment '角色
';
/*==============================================================*/
/* Table: tlink */
/*==============================================================*/
create table tlink
(
userId bigint not null,
roleId bigint not null
);
alter table tlink comment '连接表';
/*==============================================================*/
/* Table: user */
/*==============================================================*/
create table user
(
id bigint not null auto_increment,
username varchar(24),
remark varchar(240),
primary key (id)
);
alter table user comment '用户';
alter table tlink add constraint FK_r foreign key (roleId)
references role (id) on delete restrict on update restrict;
alter table tlink add constraint FK_u foreign key (userId)
references user (id) on delete restrict on update restrict;
三、POJO
/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 0:12:13<br>
* <b>Note</b>: 用户角色多对多模型:角色
*/
public class User {
private Long id;
private String username;
private String remark;
private List<Role> roleList = new ArrayList<Role>();
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", remark='" + remark + '\'' +
", roleList='" + roleList.size() + '\'' +
'}';
}
public String out() {
StringBuffer sb = new StringBuffer();
sb.append("User{" +
"id=" + id +
", username='" + username + '\'' +
", remark='" + remark + '\'' +
", roleList='" + roleList.size() + '\'');
for (Role role : roleList) {
sb.append("\n\t").append(role.toString());
}
return sb.toString();
}
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 0:12:13<br>
* <b>Note</b>: 用户角色多对多模型:角色
*/
public class User {
private Long id;
private String username;
private String remark;
private List<Role> roleList = new ArrayList<Role>();
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", remark='" + remark + '\'' +
", roleList='" + roleList.size() + '\'' +
'}';
}
public String out() {
StringBuffer sb = new StringBuffer();
sb.append("User{" +
"id=" + id +
", username='" + username + '\'' +
", remark='" + remark + '\'' +
", roleList='" + roleList.size() + '\'');
for (Role role : roleList) {
sb.append("\n\t").append(role.toString());
}
return sb.toString();
}
public class Role {
private Long id;
private String rolename;
private String descp;
private List<User> userList= new ArrayList<User>();
public String toString() {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", descp='" + descp + '\'' +
", userList=" + userList.size() +
'}';
}
public String out(){
StringBuffer sb= new StringBuffer();
if(userList.size()>0){
sb.append("Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", descp='" + descp + '\'' +
", userList=" + userList.size());
for(User u: userList){
sb.append("\n\t").append(u.toString());
}
sb.append("\n}");
}
return sb.toString();
}
private Long id;
private String rolename;
private String descp;
private List<User> userList= new ArrayList<User>();
public String toString() {
return "Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", descp='" + descp + '\'' +
", userList=" + userList.size() +
'}';
}
public String out(){
StringBuffer sb= new StringBuffer();
if(userList.size()>0){
sb.append("Role{" +
"id=" + id +
", rolename='" + rolename + '\'' +
", descp='" + descp + '\'' +
", userList=" + userList.size());
for(User u: userList){
sb.append("\n\t").append(u.toString());
}
sb.append("\n}");
}
return sb.toString();
}
/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 0:17:15<br>
* <b>Note</b>: 用户角色多对多模型:连接表
*/
public class Tlink {
private Long id;
private Long userId;
private Long roleId;
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 0:17:15<br>
* <b>Note</b>: 用户角色多对多模型:连接表
*/
public class Tlink {
private Long id;
private Long userId;
private Long roleId;
四、SqlMap
User.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="user">
<typeAlias alias="user" type="com.lavasoft.ssi.domain.User"/>
<resultMap id="result_basc" class="user">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="remark" column="remark"/>
</resultMap>
<resultMap id="result" class="user" extends="result_basc">
<result property="roleList" column="id" select="role.getByUserId"/>
</resultMap>
<insert id="insert" parameterClass="user">
insert into user(username,remark) values(#username#,#remark#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getById" parameterClass="long" resultMap="result_basc">
select * from user where id = #value#
</select>
<select id="getWithCashWithRoleList" parameterClass="long" resultMap="result">
select * from user where id = #value#
</select>
<select id="getByRoleId" parameterClass="long" resultMap="result_basc">
select u.* from user u where u.id in
(select userId from tlink where roleId=#value#)
</select>
</sqlMap>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="user">
<typeAlias alias="user" type="com.lavasoft.ssi.domain.User"/>
<resultMap id="result_basc" class="user">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="remark" column="remark"/>
</resultMap>
<resultMap id="result" class="user" extends="result_basc">
<result property="roleList" column="id" select="role.getByUserId"/>
</resultMap>
<insert id="insert" parameterClass="user">
insert into user(username,remark) values(#username#,#remark#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getById" parameterClass="long" resultMap="result_basc">
select * from user where id = #value#
</select>
<select id="getWithCashWithRoleList" parameterClass="long" resultMap="result">
select * from user where id = #value#
</select>
<select id="getByRoleId" parameterClass="long" resultMap="result_basc">
select u.* from user u where u.id in
(select userId from tlink where roleId=#value#)
</select>
</sqlMap>
Role.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="role">
<typeAlias alias="role" type="com.lavasoft.ssi.domain.Role"/>
<resultMap id="result_basc" class="role">
<result property="id" column="id"/>
<result property="rolename" column="rolename"/>
<result property="descp" column="descp"/>
</resultMap>
<resultMap id="result" class="role" extends="result_basc">
<result property="userList" column="id" select="user.getByRoleId"/>
</resultMap>
<insert id="insert" parameterClass="role">
insert into role(rolename,descp) values(#rolename#,#descp#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getById" parameterClass="long" resultMap="result_basc">
select * from role where id = #value#
</select>
<select id="getRoleByIdWithCashUser" parameterClass="long" resultMap="result">
select * from role where id = #value#
</select>
<!--为多对多配置-->
<select id="getByUserId" parameterClass="long" resultClass="role" resultMap="result_basc">
select r.* from role r where r.id in
(select roleId from tlink where userId=#value#)
</select>
</sqlMap>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="role">
<typeAlias alias="role" type="com.lavasoft.ssi.domain.Role"/>
<resultMap id="result_basc" class="role">
<result property="id" column="id"/>
<result property="rolename" column="rolename"/>
<result property="descp" column="descp"/>
</resultMap>
<resultMap id="result" class="role" extends="result_basc">
<result property="userList" column="id" select="user.getByRoleId"/>
</resultMap>
<insert id="insert" parameterClass="role">
insert into role(rolename,descp) values(#rolename#,#descp#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getById" parameterClass="long" resultMap="result_basc">
select * from role where id = #value#
</select>
<select id="getRoleByIdWithCashUser" parameterClass="long" resultMap="result">
select * from role where id = #value#
</select>
<!--为多对多配置-->
<select id="getByUserId" parameterClass="long" resultClass="role" resultMap="result_basc">
select r.* from role r where r.id in
(select roleId from tlink where userId=#value#)
</select>
</sqlMap>
Tlink.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="tlink">
<typeAlias alias="tlink" type="com.lavasoft.ssi.domain.Tlink"/>
<resultMap id="result" class="tlink">
<result property="id" column="id"/>
<result property="userId" column="userId"/>
<result property="roleId" column="roleId"/>
</resultMap>
<insert id="insert" parameterClass="tlink">
insert into tlink(userId,roleId) values(#userId#,#roleId#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getByUserId" parameterClass="long" resultMap="result">
select * from tlink where userId = #value#
</select>
<select id="getByRoleId" parameterClass="long" resultMap="result">
select * from tlink where roleId = #value#
</select>
<delete id="delete" parameterClass="tlink">
delete from tlink where userId = #userId# and roleId = #roleId#
</delete>
</sqlMap>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="tlink">
<typeAlias alias="tlink" type="com.lavasoft.ssi.domain.Tlink"/>
<resultMap id="result" class="tlink">
<result property="id" column="id"/>
<result property="userId" column="userId"/>
<result property="roleId" column="roleId"/>
</resultMap>
<insert id="insert" parameterClass="tlink">
insert into tlink(userId,roleId) values(#userId#,#roleId#)
<selectKey keyProperty="id" resultClass="long">
select LAST_INSERT_ID()
</selectKey>
</insert>
<select id="getByUserId" parameterClass="long" resultMap="result">
select * from tlink where userId = #value#
</select>
<select id="getByRoleId" parameterClass="long" resultMap="result">
select * from tlink where roleId = #value#
</select>
<delete id="delete" parameterClass="tlink">
delete from tlink where userId = #userId# and roleId = #roleId#
</delete>
</sqlMap>
五、DAO
public interface UserDAO {
public Long insert(User user);
public Object getById(Long id);
public Object getWithCashById(Long id);
public User getWithCashWithRoleList(Long id);
}
public Long insert(User user);
public Object getById(Long id);
public Object getWithCashById(Long id);
public User getWithCashWithRoleList(Long id);
}
public interface RoleDAO {
public Long insert(Role role);
public Role getById(Long id);
public Role getRoleByIdWithCashUser(Long id);
public List<Role> getByUserId(Long userId);
}
public Long insert(Role role);
public Role getById(Long id);
public Role getRoleByIdWithCashUser(Long id);
public List<Role> getByUserId(Long userId);
}
public interface TlinkDAO {
public void insert(Long userId,Long roleId);
public int delete(Long userId,Long roleId);
public int update(Long userId,Long roleId);
}
public void insert(Long userId,Long roleId);
public int delete(Long userId,Long roleId);
public int update(Long userId,Long roleId);
}
public class UserDAOImpl extends SqlMapClientDaoSupport implements UserDAO {
public Long insert(User user) {
return (Long) getSqlMapClientTemplate().insert("user.insert",user);
}
public Object getById(Long id) {
return getSqlMapClientTemplate().queryForList("user.getById",id);
}
public Object getWithCashById(Long id) {
return getSqlMapClientTemplate().queryForList("user.getWithCashById",id);
}
public User getWithCashWithRoleList(Long userId) {
return (User) getSqlMapClientTemplate().queryForObject("user.getWithCashWithRoleList",userId);
}
}
public Long insert(User user) {
return (Long) getSqlMapClientTemplate().insert("user.insert",user);
}
public Object getById(Long id) {
return getSqlMapClientTemplate().queryForList("user.getById",id);
}
public Object getWithCashById(Long id) {
return getSqlMapClientTemplate().queryForList("user.getWithCashById",id);
}
public User getWithCashWithRoleList(Long userId) {
return (User) getSqlMapClientTemplate().queryForObject("user.getWithCashWithRoleList",userId);
}
}
public class RoleDAOImpl extends SqlMapClientDaoSupport implements RoleDAO {
public Long insert(Role role) {
return (Long) getSqlMapClientTemplate().insert("role.insert",role);
}
public Role getById(Long id) {
return (Role) getSqlMapClientTemplate().queryForObject("role.getById",id);
}
public Role getRoleByIdWithCashUser(Long id) {
return (Role) getSqlMapClientTemplate().queryForObject("role.getRoleByIdWithCashUser",id);
}
public List<Role> getByUserId(Long userId) {
return getSqlMapClientTemplate().queryForList("role.getByUserId",userId);
}
}
public Long insert(Role role) {
return (Long) getSqlMapClientTemplate().insert("role.insert",role);
}
public Role getById(Long id) {
return (Role) getSqlMapClientTemplate().queryForObject("role.getById",id);
}
public Role getRoleByIdWithCashUser(Long id) {
return (Role) getSqlMapClientTemplate().queryForObject("role.getRoleByIdWithCashUser",id);
}
public List<Role> getByUserId(Long userId) {
return getSqlMapClientTemplate().queryForList("role.getByUserId",userId);
}
}
public class TlinkDAOImpl extends SqlMapClientDaoSupport implements TlinkDAO {
public void insert(Long userId, Long roleId) {
Tlink tlink = new Tlink(userId, roleId);
getSqlMapClientTemplate().insert("tlink.insert",tlink);
}
public int delete(Long userId, Long roleId) {
Tlink tlink = new Tlink(userId, roleId);
return getSqlMapClientTemplate().delete("tlink.delete",tlink);
}
public int update(Long userId, Long roleId) {
return 0;
}
}
public void insert(Long userId, Long roleId) {
Tlink tlink = new Tlink(userId, roleId);
getSqlMapClientTemplate().insert("tlink.insert",tlink);
}
public int delete(Long userId, Long roleId) {
Tlink tlink = new Tlink(userId, roleId);
return getSqlMapClientTemplate().delete("tlink.delete",tlink);
}
public int update(Long userId, Long roleId) {
return 0;
}
}
六、测试类
public class UserDAOTest{
private UserDAO userDAO = (UserDAO) ApplicationContextUtils.getApplicationContext().getBean("userDAO");
public void testInsert() {
System.out.println("-------insert(User user)--------");
User user = new User();
user.setUsername("熔岩");
user.setRemark("系统用户");
Long pk = userDAO.insert(user);
System.out.println("所插入数据ID=" + pk);
}
public void testGetById() {
System.out.println("-------getById(Long id)-------");
Object object = userDAO.getById(3L);
System.out.println(object);
}
public void testGetWithCashById() {
System.out.println("-------getWithCashById(Long id)-------");
Object object = userDAO.getWithCashById(1L);
System.out.println(object);
}
public void getWithCashWithRoleList(){
System.out.println("-------getWithCashWithRoleList()-------");
User user = userDAO.getWithCashWithRoleList(1L);
System.out.println(user.out());
}
public static void main(String args[]){
System.out.println("正在测试UserDAO");
UserDAOTest userDAOTest = new UserDAOTest();
userDAOTest.testInsert();
userDAOTest.testGetById();
userDAOTest.getWithCashWithRoleList();
}
}
private UserDAO userDAO = (UserDAO) ApplicationContextUtils.getApplicationContext().getBean("userDAO");
public void testInsert() {
System.out.println("-------insert(User user)--------");
User user = new User();
user.setUsername("熔岩");
user.setRemark("系统用户");
Long pk = userDAO.insert(user);
System.out.println("所插入数据ID=" + pk);
}
public void testGetById() {
System.out.println("-------getById(Long id)-------");
Object object = userDAO.getById(3L);
System.out.println(object);
}
public void testGetWithCashById() {
System.out.println("-------getWithCashById(Long id)-------");
Object object = userDAO.getWithCashById(1L);
System.out.println(object);
}
public void getWithCashWithRoleList(){
System.out.println("-------getWithCashWithRoleList()-------");
User user = userDAO.getWithCashWithRoleList(1L);
System.out.println(user.out());
}
public static void main(String args[]){
System.out.println("正在测试UserDAO");
UserDAOTest userDAOTest = new UserDAOTest();
userDAOTest.testInsert();
userDAOTest.testGetById();
userDAOTest.getWithCashWithRoleList();
}
}
/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 1:05:57<br>
* <b>Note</b>: 用户角色多对多模型:角色
*/
public class RoleDAOTest{
RoleDAO roleDAO = (RoleDAO) ApplicationContextUtils.getApplicationContext().getBean("roleDAO");
public void testInsert() {
System.out.println("------insert(Role role)-----");
Role role = new Role();
role.setRolename("admin");
role.setDescp("管理员");
Long pk = roleDAO.insert(role);
System.out.println("所插入数据ID="+pk);
}
public void testGetById() {
System.out.println("------getById(Long id)-----");
Object obj = roleDAO.getById(1L);
System.out.println("查询结果"+obj);
}
public void testGetRoleByIdWithCashUser() {
System.out.println("------getRoleByIdWithCashUser(Long id)-----");
Role obj = roleDAO.getRoleByIdWithCashUser(1L);
System.out.println("查询结果"+obj.out());
}
public void testGetByUserId(){
System.out.println("------getByUserId(Long userId)-----");
List<Role> roleList= roleDAO.getByUserId(1L);
for(Role r: roleList){
System.out.println(r);
}
}
public static void main(String args[]){
System.out.println("正在测试RoleDAO");
RoleDAOTest roleDAOTest = new RoleDAOTest();
roleDAOTest.testInsert();
roleDAOTest.testGetById();
roleDAOTest.testGetRoleByIdWithCashUser();
roleDAOTest.testGetByUserId();
}
}
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-6-16 1:05:57<br>
* <b>Note</b>: 用户角色多对多模型:角色
*/
public class RoleDAOTest{
RoleDAO roleDAO = (RoleDAO) ApplicationContextUtils.getApplicationContext().getBean("roleDAO");
public void testInsert() {
System.out.println("------insert(Role role)-----");
Role role = new Role();
role.setRolename("admin");
role.setDescp("管理员");
Long pk = roleDAO.insert(role);
System.out.println("所插入数据ID="+pk);
}
public void testGetById() {
System.out.println("------getById(Long id)-----");
Object obj = roleDAO.getById(1L);
System.out.println("查询结果"+obj);
}
public void testGetRoleByIdWithCashUser() {
System.out.println("------getRoleByIdWithCashUser(Long id)-----");
Role obj = roleDAO.getRoleByIdWithCashUser(1L);
System.out.println("查询结果"+obj.out());
}
public void testGetByUserId(){
System.out.println("------getByUserId(Long userId)-----");
List<Role> roleList= roleDAO.getByUserId(1L);
for(Role r: roleList){
System.out.println(r);
}
}
public static void main(String args[]){
System.out.println("正在测试RoleDAO");
RoleDAOTest roleDAOTest = new RoleDAOTest();
roleDAOTest.testInsert();
roleDAOTest.testGetById();
roleDAOTest.testGetRoleByIdWithCashUser();
roleDAOTest.testGetByUserId();
}
}
public class TlinkDAOTest {
TlinkDAO tlinkDAO = (TlinkDAO) ApplicationContextUtils.getApplicationContext().getBean("tlinkDAO");
public void testInsert(){
tlinkDAO.insert(1L,1L);
tlinkDAO.insert(1L,2L);
tlinkDAO.insert(1L,3L);
}
public static void main(String args[]){
TlinkDAOTest tlinkDAOTest = new TlinkDAOTest();
tlinkDAOTest.testInsert();
}
}
TlinkDAO tlinkDAO = (TlinkDAO) ApplicationContextUtils.getApplicationContext().getBean("tlinkDAO");
public void testInsert(){
tlinkDAO.insert(1L,1L);
tlinkDAO.insert(1L,2L);
tlinkDAO.insert(1L,3L);
}
public static void main(String args[]){
TlinkDAOTest tlinkDAOTest = new TlinkDAOTest();
tlinkDAOTest.testInsert();
}
}
七、测试结果
正在测试UserDAO
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
-------insert(User user)--------
所插入数据ID=10
-------getById(Long id)-------
[User{id=3, username='熔岩', remark='系统用户', roleList='0'}]
-------getWithCashWithRoleList()-------
User{id=1, username='熔岩', remark='系统用户', roleList='3'
Role{id=1, rolename='r1', descp='aaa', userList=0}
Role{id=2, rolename='r2', descp='bbb', userList=0}
Role{id=3, rolename='r3', descp='ccc', userList=0}
Process finished with exit code 0
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
-------insert(User user)--------
所插入数据ID=10
-------getById(Long id)-------
[User{id=3, username='熔岩', remark='系统用户', roleList='0'}]
-------getWithCashWithRoleList()-------
User{id=1, username='熔岩', remark='系统用户', roleList='3'
Role{id=1, rolename='r1', descp='aaa', userList=0}
Role{id=2, rolename='r2', descp='bbb', userList=0}
Role{id=3, rolename='r3', descp='ccc', userList=0}
Process finished with exit code 0
正在测试RoleDAO
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
------insert(Role role)-----
所插入数据ID=10
------getById(Long id)-----
查询结果Role{id=1, rolename='r1', descp='aaa', userList=0}
------getRoleByIdWithCashUser(Long id)-----
查询结果Role{id=1, rolename='r1', descp='aaa', userList=3
User{id=1, username='熔岩', remark='系统用户', roleList='0'}
User{id=2, username='熔岩', remark='系统用户', roleList='0'}
User{id=3, username='熔岩', remark='系统用户', roleList='0'}
}
------getByUserId(Long userId)-----
Role{id=1, rolename='r1', descp='aaa', userList=0}
Role{id=2, rolename='r2', descp='bbb', userList=0}
Role{id=3, rolename='r3', descp='ccc', userList=0}
Process finished with exit code 0
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
------insert(Role role)-----
所插入数据ID=10
------getById(Long id)-----
查询结果Role{id=1, rolename='r1', descp='aaa', userList=0}
------getRoleByIdWithCashUser(Long id)-----
查询结果Role{id=1, rolename='r1', descp='aaa', userList=3
User{id=1, username='熔岩', remark='系统用户', roleList='0'}
User{id=2, username='熔岩', remark='系统用户', roleList='0'}
User{id=3, username='熔岩', remark='系统用户', roleList='0'}
}
------getByUserId(Long userId)-----
Role{id=1, rolename='r1', descp='aaa', userList=0}
Role{id=2, rolename='r2', descp='bbb', userList=0}
Role{id=3, rolename='r3', descp='ccc', userList=0}
Process finished with exit code 0
测试结果表明:双向多对多彻底搞定!
本文出自 “熔 岩” 博客,转载请与作者联系!
本文出自 51CTO.COM技术博客发表评论
-
memcached在windows下多实例并存
2009-06-02 16:33 2405memcached.exe的-d install命令安装时其他 ... -
Hibernate shard介绍
2009-02-11 11:03 1140hibernate shards源自google的 ... -
学习ibatis的一些资料
2009-02-10 14:45 73731.官方网址: http://ibatis.apache.or ... -
spring的加载过程(web) (12)--refreshBeanFactory
2008-12-08 14:25 1699spring的加载过程(web) (12)--refreshB ... -
spring的加载过程(web) (11)--publishEvent
2008-12-01 17:29 1773spring的加载过程(web) (11)--publishE ... -
spring的加载过程(web) (10)--preInstantiateSingletons
2008-12-01 17:23 4714spring的加载过程(web) (10)--preInsta ... -
spring的加载过程(web) (9)--registerListeners
2008-12-01 16:32 1484spring的加载过程(web) (9)--registerL ... -
spring的加载过程(web) (8)--onRefresh
2008-12-01 15:22 1689spring的加载过程(web) (8)--onRefresh ... -
spring的加载过程(web) (7)--initApplicationEventMulticas
2008-12-01 14:33 1318spring的加载过程(web) (7)--initAppli ... -
spring的加载过程(web) (6)--initMessageSource
2008-12-01 11:48 1501spring的加载过程(web) (6)--initMessa ... -
spring的加载过程(web) (5)--registerBeanPostProcessors
2008-12-01 11:27 1689spring的加载过程(web) (5)--registerB ... -
spring的加载过程(web) (4)--invokeBeanFactoryPostProcess
2008-11-28 17:29 1537spring的加载过程(web) (4)--invokeBea ... -
spring的加载过程(web) (3)--postProcessBeanFactory
2008-11-28 11:35 5830spring的加载过程(web) (3)--postProce ... -
spring的加载过程(web) (2)--prepareBeanFactory
2008-11-26 16:14 2193prepareBeanFactory是配置factory的标 ... -
spring的加载过程(web) (1)
2008-11-26 14:13 2935自己看源码总结的不一定准确,记录下来以备以后查看。 1.通过w ... -
Spring 2.0 AOP 与事务配置
2008-09-24 19:57 1098Spring 1.0的标准事务配置 先定义一个baseTxSe ...
相关推荐
包括了几部分: ...7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis insert操作陷阱.doc 每章都有小例子。 呵呵,希望有所帮助!
本文将深入探讨如何在iBATIS中实现一对多和多对多的关系映射,并提供相关的源码分析和工具使用技巧。 **一对多关系映射** 在数据库设计中,一对多关系是指一个父记录可以与多个子记录关联,例如,一个用户可以有多...
文件名如 "iBatis2学习笔记:多对多映射(双向).htm" 暗示了文档可能深入讲解了如何配置和使用双向多对多映射,而 "iBatis2学习笔记:SqlMap的配置总结(18条).htm" 可能提供了关于 iBatis 配置的全面概述。...
Ibatis,作为一个轻量级的Java ORM框架,提供了强大的功能来处理复杂的数据映射,其中包括一对多的关系映射。在这个场景下,我们将深入探讨Ibatis如何实现一对多的关系映射。 一对多关系在数据库设计中非常常见,它...
这一改变使得iBATIS v3能够更灵活地处理多个映射文件,同时也增强了框架的可扩展性。 #### sqlMap 映射中的异同 - **DTD定义:** - **iBATIS v1**:使用的是`sql-map.dtd`。 - **iBATIS v2**:采用`sql-map-2....
### ibatis教程学习笔记 #### 一、ibatis简介与特点 ibatis 是一个基于 Java 的持久层框架,它提供了一种将 SQL 映射到 Java 对象的方式,简化了 JDBC 的复杂操作。ibatis 通过 XML 配置文件或者注解的形式来描述 ...
2. 学习笔记:个人对Ibatis的理解和实践经验,包括常见问题解决方案、最佳实践等。 3. 示例代码:实际运行的Ibatis应用示例,有助于理解Ibatis如何在项目中工作。 4. 比较分析:关于Ibatis与其他框架(如Hibernate、...
ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记
集合映射适用于一对多的关系,通过在XML映射文件中配置resultMap,可以将主表的一条记录映射成多个从表的对象集合。关联映射则用于一对一或一对多的关系,通过nestedResultMap或association标签,可以实现主从表的...
IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得
2. 执行映射的SQL语句,iBATIS会创建PreparedStatement实例,利用对象参数执行SQL,并从ResultSet构建返回的对象。 3. 更新操作时返回受影响的行数,查询操作时返回单个对象或对象集合。 **Data Access Objects ...
本篇将深入讲解iBATIS一对一映射的概念、配置及应用,帮助你更好地理解和运用这个功能。 一对一映射在数据库设计中是指两个表之间存在一对一的关系,例如,一个员工可能只有一个部门,一个部门也只对应一个员工。在...
### iBatis 学习笔记知识点总结 #### 一、iBatis 概念与特点 **1.1 iBatis 定义** - **iBatis** 是一个基于 Java 的开源持久层框架,它专注于 SQL 映射,提供了一种将对象与数据库交互过程中的 SQL 语句进行分离的...
iBatis多对多关系详解 iBatis是一种流行的持久层框架,用于简化Java应用程序...我们使用了三个数据库表来存储学生和教师之间的多对多关系,并使用iBatis来映射数据库表和Java对象。最后,我们实现了DAO来访问数据库。
总结来说,这个话题覆盖了Mybatis/iBatis中的多表映射,通过XML配置文件处理一对一和一对多关系,以及如何在ExtJS前端通过Model和Grid获取并展示这些数据。了解这些知识对于开发涉及多表交互的应用至关重要,能够...
通过本文的学习笔记,我们可以了解到 iBatis 在简化数据库访问的同时提供了足够的灵活性。尽管 iBatis 相比 Hibernate 在自动化程度上略显不足,但对于需要高度定制 SQL 查询的场景来说,iBatis 的优势十分明显。...
四、iBatis2.x_jar包 iBatis 2.x版本是iBatis历史上的一个里程碑,其中包含了许多基础库和核心组件。这个jar包包含了以下主要部分: - iBatis核心库:提供SQL映射和数据访问的功能。 - SQLMapClient:用于创建和管理...
### IBATIS学习笔记知识点详解 #### 一、IBATIS简介 iBatis是一个用于Java的数据持久化框架,类似于Hibernate、JDO和EJB等技术。它的主要特点是将对象映射为SQL语句,这使得开发人员可以更加灵活地控制SQL的执行,...
这篇“ibatis学习笔记(一)”可能是博主对Ibatis基础概念、安装配置以及基本使用的介绍,让我们通过标签“源码”和“工具”来深入探讨Ibatis的相关知识。 首先,Ibatis是一个轻量级的Java ORM(对象关系映射)框架...