One-to-one and one-to-one implementations in ibatis3

Source: Internet
Author: User

1. One-to-many



1. DB


Character Table t_user
Id bigint
Name varchar (20)
Age int

Item table t_user_goods
Id bigint
Goods_name varchar (20)
Uid bigint

2. sqlmap-config.xml



<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE configuration PUBLIC "-// ibatis.apache.org//DTD Config 3.0 //" http://ibatis.apache.org/dtd/ibatis-3-config.dtd ">
<Configuration>
<TypeAliases>
<TypeAlias type = "com. test. model. User" alias = "user"/>
<TypeAlias type = "com. test. model. UserGoods" alias = "userGoods"/>
</TypeAliases>
<Environments default = "development">
<Environment id = "development">
<TransactionManager type = "JDBC"/>
<DataSource type = "UNPOOLED">
<Property name = "driver"
Value = "net. sourceforge. jtds. jdbc. Driver"/>
<Property name = "url"
Value = "jdbc: jtds: sqlserver: // 192.168.0.101: 1433/guagua_vas_stock"/>
<Property name = "username" value = "guagua_web"/>
<Property name = "password" value = "123456"/>
</DataSource>
</Environment>
</Environments>
<Mappers>
<Mapper resource = "com/test/model/User. xml"/>
</Mappers>
</Configuration>

 3. User. java


Package com. test. model;

Import java. io. Serializable;
Import java. util. List;

Public class User implements Serializable {
Private static final long serialVersionUID = 8681358563101101660L;

Private long id;
Private String name;
Private int age;
Private List <UserGoods> goods;

--- Getter setter saving
}

4. UserGoods. java


Package com. test. model;

Import java. io. Serializable;

Public class UserGoods implements Serializable {

Private static final long serialVersionUID =-20173573481231610000l;
Private long id;
Private String name;
Private long uid;

-- Getter setter saving
}

 5. User. xml


<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper PUBLIC "-// ibatis.apache.org//DTD Mapper 3.0 // EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<Mapper namespace = "user">
<ResultMap type = "user" id = "userMapper">
<Result property = "id" column = "id" jdbcType = "BIGINT"/>
<Result property = "name" column = "name" jdbcType = "VARCHAR"/>
<Result property = "age" column = "age" jdbcType = "INTEGER"/>
<Collection property = "goods" ofType = "java. util. List" select = "getGoodsById" column = "id"/>



</ResultMap>

<ResultMap type = "userGoods" id = "userGoodsMapper">
<Result property = "id" column = "id" jdbcType = "BIGINT"/>
<Result property = "name" column = "goods_name" jdbcType = "VARCHAR"/>
<Result property = "uid" column = "uid" jdbcType = "BIGINT"/>
</ResultMap>

<! -- Get the UserGoods object -->
<Select id = "getGoodsById" parameterType = "Integer" resultMap = "userGoodsMapper">
Select * from t_user_goods where uid =#{ id}
</Select>

<Select id = "getUserList" resultMap = "userMapper">
Select * from t_user
</Select>
</Mapper>

Note: resultMap does not seem to support cross-file use.

6. UserDao. java


Package com. test. dao;

Import java. util. List;
Import org. apache. ibatis. session. SqlSession;
Import com. bd. dao. IbatisSessionFactory;
Import com. test. model. User;

Public class UserDao {

Public List <User> getUserList (){
SqlSession session = IbatisSessionFactory. getSqlSession ();
List <User> list = (List) session. selectList ("getUserList ");
Session. commit ();
Session. close ();
Return list;
}

}

2. One-to-one


Assume that t_user and t_user_goods are in a one-to-one relationship.
 1. User. java


Package com. test. model;

Import java. io. Serializable;

Public class User implements Serializable {
Private static final long serialVersionUID = 8681358563101101660L;

Private long id;
Private String name;
Private int age;
Private UserGoods goods; // changed from List to UserGoods

-- Getter setter
}

2. User. xml


<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE mapper PUBLIC "-// ibatis.apache.org//DTD Mapper 3.0 // EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<Mapper namespace = "user">
<ResultMap type = "user" id = "userMapper">
<Result property = "id" column = "id" jdbcType = "BIGINT"/>
<Result property = "name" column = "name" jdbctype = "varchar"/>
<Result property = "Age" column = "Age" jdbctype = "integer"/>
<! -- Collection property = "goods" oftype = "Java. util. List" select = "getgoodsbyid" column = "ID"/-->
<Association property = "goods" column = "id" select = "getGoodsById"/>


</Resultmap>

<Resultmap type = "usergoods" id = "usergoodsmapper">
<Result property = "ID" column = "ID" jdbctype = "bigint"/>
<Result property = "name" column = "goods_name" jdbctype = "varchar"/>
<Result property = "uid" column = "uid" jdbctype = "bigint"/>
</Resultmap>

<! -- Get the usergoods object -->
<Select id = "getgoodsbyid" parametertype = "integer" resultmap = "usergoodsmapper">
Select * From t_user_goods where uid =#{ ID}
</SELECT>

<Select id = "getUserById" parameterType = "int" resultMap = "userMapper">
Select * from t_user where id =#{ id}
</Select>
</Mapper>

3. UserDao. java


Package com. test. dao;

Import java. util. List;
Import org. apache. ibatis. session. SqlSession;
Import com. bd. dao. IbatisSessionFactory;
Import com. test. model. User;

Public class UserDao implements UserDaoInte {
Public User getUserById (Long id ){
SqlSession session = IbatisSessionFactory. getSqlSession ();
User user = (User) session. selectOne ("getUserById", id );
Session. commit ();
Session. close ();

Return user;
}
}


4. Test code


Public static void main (String [] args ){
UserDao dao = new UserDao ();

User u = dao. getUserById (2L );
System. out. println ("name:" + u. getName ());
System. out. println ("goods:" + u. getGoods (). getName ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.