Mybatis 浠g爜瀹炶返
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 浠g爜瀹炶返相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/stream' title='stream'>stream
Mybatis 鏄粈涔堬紵
Mybatis 鏄竴涓寔涔呭眰妗嗘灦銆侸ava 涓€氳繃 Mybatis 锛岀▼搴忓彲浠ュ緢鏂逛究鐨勫鏁版嵁搴撹繘琛屾搷浣溿€備娇寮€鍙戣€呬笓娉ㄤ簬 SQL璇彞锛岄伩鍏嶄簡閲嶅鐨?JDBC 浠g爜銆?/p>
- 浣跨敤 MyBatis 閬垮厤浜嗗嚑涔庢墍鏈夌殑 JDBC 浠g爜鍜屾墜鍔ㄨ缃弬鏁颁互鍙婅幏鍙栫粨鏋滈泦
- 鐏垫椿涓斿己澶э紝鏀寔瀹氬埗鍖?SQL銆佸瓨鍌ㄨ繃绋嬩互鍙婇珮绾ф槧灏?/li>
- 鏀寔 XML 鍜屾敞瑙d袱绉嶅舰寮忕紪鍐?SQL
- 鏁版嵁搴撲腑鐨勮褰曞彲浠ユ槧灏勪负鏅€?Java 瀵硅薄
鍑嗗
鐜锛?/strong>
寮€鍙戝伐鍏凤細IDEA
鍖呯鐞嗭細Maven
JDK锛?.8
Mybatis 锛歁ybatis 3.4.6
闂鍦烘櫙
浣跨敤 Mybatis 瀹屾垚鍩轰簬绠€鍗曟潈闄愯璁$殑鏁版嵁搴撹〃鐨勭浉鍏虫搷浣溿€佹煡璇?/p>
娑夊強鍒扮殑琛?/p>
t_user
t_user_info
t_role
t_menu
t_user_role //鍏崇郴琛?t_role_menu //鍏崇郴琛?/code>
瑕佸仛鍝簺鎿嶄綔
- t_user 琛ㄧ殑绠€鍗曞鍒犳敼鎴?/li>
- 鍏宠仈澶嶆潅鏌ヨ锛屾煡璇竴涓敤鎴风殑鎵€鏈夋潈闄愪俊鎭?/li>
- 鍔ㄦ€佹煡璇紝鍖呭惈鏌愪簺鑿滃崟鏉冮檺鐨勭敤鎴?/li>
1銆佹柊寤烘暟鎹簱琛紝鍒濆鍖栦簺鏁版嵁
-- ----------------------------
-- Table structure for `t_menu`
-- ----------------------------
DROP TABLE IF EXISTS `t_menu`;
CREATE TABLE `t_menu` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`url` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `t_role`
-- ----------------------------
DROP TABLE IF EXISTS `t_role`;
CREATE TABLE `t_role` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `t_role_menu`
-- ----------------------------
DROP TABLE IF EXISTS `t_role_menu`;
CREATE TABLE `t_role_menu` (
`role_id` int(10) DEFAULT NULL,
`menu_id` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `t_user`
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`nickname` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `t_user_username_uindex` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `t_user_role`
-- ----------------------------
DROP TABLE IF EXISTS `t_user_role`;
CREATE TABLE `t_user_role` (
`user_id` int(10) DEFAULT NULL,
`role_id` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;
-- ----------------------------
-- Table structure for `t_user_info`
-- ----------------------------
DROP TABLE IF EXISTS `t_user_info`;
CREATE TABLE `t_user_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`name` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`email` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`address` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`phone` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`sex` varchar(2) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
SET FOREIGN_KEY_CHECKS = 1;
鎵嬪姩鍒濆鍖栦簺鏁版嵁杩涘幓锛屾瀯寤轰竴浜涘叧鑱旀暟鎹紝涓轰笅闈㈡祴璇曟墍鐢?/p>
2銆佹瀯寤篗aven椤圭洰
缁勭粐 project 鐩綍
鈹斺攢鈹€ src
鈹溾攢鈹€ main
鈹?? 鈹溾攢鈹€ java
鈹?? 鈹?? 鈹斺攢鈹€ me
鈹?? 鈹?? 鈹斺攢鈹€ imcoding
鈹?? 鈹?? 鈹溾攢鈹€ dao
鈹?? 鈹?? 鈹溾攢鈹€ mapper
鈹?? 鈹?? 鈹溾攢鈹€ model
鈹?? 鈹斺攢鈹€ resources
鈹?? 鈹斺攢鈹€ mapper
鈹斺攢鈹€ test
鈹斺攢鈹€ java
鈹斺攢鈹€ me
鈹斺攢鈹€ imcoding
3銆佸紩鍏?Mybatis 鐩稿叧渚濊禆
鍦?maven 鐜涓紝闇€瑕佸湪 pom.xml 鏂囦欢涓鍔犱緷璧?/p>
<dependencies>
<!--寮曞叆 Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 寮曞叆 mysql 椹卞姩-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- JSON 宸ュ叿鍖?->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<!-- 鍗曞厓娴嬭瘯 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
4銆侀厤缃?Mybatis
鍦?resources 鐩綍鏂板缓 mybatis-config.xml
鈹斺攢鈹€ resources
鈹溾攢鈹€ mapper
鈹斺攢鈹€ mybatis-config.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>
<!--璁剧疆properties 灞炴€ф枃浠?->
<properties resource="dbconfig.properties"></properties>
<!--榛樿浣跨敤鐨勯厤缃幆澧冿細development-->
<environments default="development">
<!--寮€鍙戠幆澧冮厤缃?->
<environment id="development">
<!--浜嬪姟绠$悊鍣ㄧ殑閰嶇疆-->
<transactionManager type="JDBC"/>
<!--閰嶇疆鏁版嵁婧?->
<dataSource type="POOLED">
<!--鍔ㄦ€佹浛鎹㈠睘鎬у€?->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 杩欓噷寮曞叆 Mapper 鏂囦欢-->
</mappers>
</configuration>
澶栭儴灞炴€ч厤缃紝resources 鐩綍涓嬫柊澧?dbconfig.properties 鏂囦欢
鈹斺攢鈹€ resources
鈹溾攢鈹€ dbconfig.properties
鈹溾攢鈹€ mapper
鈹斺攢鈹€ mybatis-config.xml
閰嶇疆鍐呭锛?/p>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis-demo?useUnicode=true&characterEncoding=utf8
username=root
password=root
鏁版嵁搴撹繛鎺ョ殑鐩稿叧灞炴€т俊鎭?/p>
5銆佽幏鍙? SqlSession
鏍稿績浠g爜锛?/p>
// 閰嶇疆鏂囦欢浣嶇疆
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = factory.openSession();
鍐欎釜宸ュ叿绫?MybatisUtils锛?/p>
鈹溾攢鈹€ java
?? 鈹斺攢鈹€ me
? 鈹斺攢鈹€ imcoding
鈹溾攢鈹€ App.java
? 鈹溾攢鈹€ MybatisUtils.java
public class MybatisUtils {
private static SqlSessionFactory factory;
/**
* 鏍规嵁 mybatis-config.xml 閰嶇疆鍒濆鍖?factory
*/
private static void initialFactory() {
String resource = "mybatis-config.xml";
try {
InputStream in = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession() {
if (factory == null) {
initialFactory();
}
// factory.openSession(true) - 寮€鍚簨鍔¤嚜鍔ㄦ彁浜? SqlSession sqlSession = factory.openSession();
return sqlSession;
}
}
鍩烘湰澧炲垹鏀规煡锛圕URD锛?/h2>
1銆佹瀯寤?Model 绫?/h3>
鐩綍锛?/p>
鈹溾攢鈹€ model
?? 鈹溾攢鈹€ Menu.java
? 鈹溾攢鈹€ Role.java
鈹斺攢鈹€ User.java
User
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
/* setter 鍜?getter 鐪佺暐 */
}
Role
public class Role {
private Integer id;
private String name;
// setter 鍜?getter 鏂规硶鐪佺暐...
}
Menu
public class Menu {
private Integer id;
private String name;
private int type;
private String url;
// setter and getter ...
}
UserInfo
public class UserInfo {
private Integer id;
private String name;
private String sex;
private String address;
private String phone;
private String email;
//setter and getter....
}
2. 鏋勫缓 Mapper 鎺ュ彛
鐩綍锛?/p>
鈹溾攢鈹€ mapper
?? 鈹斺攢鈹€ UserMapper.java
UserMapper
public interface UserMapper {
// 鏂板 User
int insertUser(User user);
// 鏍规嵁涓婚敭鍒犻櫎
int deleteUser(int id);
// 鏇存柊 User
int updateUser(User user);
// 鏍规嵁涓婚敭 id 鏌ヨ
User selectByKey(int id);
// 鏍规嵁鐢ㄦ埛鍚嶅拰瀵嗙爜鏌ヨ
User selectByUsernameAndPassword(String username, String password);
}
3銆佸畾涔?Mapper xml
鏂板缓 UserMapper.xml
鈹斺攢鈹€ resources
鈹溾攢鈹€ dbconfig.properties
鈹溾攢鈹€ mapper
鈹?? 鈹斺攢鈹€ UserMapper.xml
鈹斺攢鈹€ mybatis-config.xml
UserMapper.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 鎺ュ彛-->
<mapper namespace="me.imcoding.mapper.UserMapper">
<!--
瀵瑰簲 UserMapper 鎺ュ彛涓殑 insertUser()
id 瑕佸搴旀帴鍙d腑鐨勬柟娉曞悕
parameterType: 鎸囧畾鍙傛暟绫诲瀷锛屽彲浠ョ渷鐣ワ紝鑷姩閫傞厤
t_user 琛ㄨ缃富閿?id 鑷姩鏂板
-->
<insert id="insertUser" parameterType="me.imcoding.model.User">
insert into t_user(username, password, nickname)
values (#{username}, #{password}, #{nickname})
</insert>
<!--瀵瑰簲 UserMapper 鎺ュ彛涓殑 deleteUser()-->
<delete id="deleteUser" >
delete from t_user where id = #{id}
</delete>
<!--
瀵瑰簲 UserMapper 鎺ュ彛涓殑 updateUser()
<set> 鍙互鑷姩澶勭悊鍔ㄦ€丼QL璇彞锛岄伩鍏嶆嫾鎺ヨ鍙ユ椂閫犳垚鐨勮娉曢敊璇? -->
<update id="updateUser" parameterType="me.imcoding.model.User">
update t_user
<set>
<if test="username != null">username = #{username}</if>
<if test="password != null">password = #{password}</if>
<if test="nickname != null">nickname = #{nickname}</if>
</set>
where id = #{id}
</update>
<!--瀵瑰簲 UserMapper 鎺ュ彛涓殑 selectUser()-->
<select id="selectUser" resultType="me.imcoding.model.User">
select
nickname,
username,
password
from t_user
where id = #{id}
</select>
<!--
瀵瑰簲 UserMapper 鎺ュ彛涓殑 selectByUsernameAndPassword()
<where> 鍙互鑷姩澶勭悊鍔ㄦ€丼QL璇彞锛岄伩鍏嶆嫾鎺ヨ鍙ユ椂閫犳垚鐨勮娉曢敊璇? -->
<select id="selectByUsernameAndPassword" resultType="me.imcoding.model.User">
select
nickname,
username,
password
from t_user
<where>
<if test="username != null">
username = #{username}
</if>
<if test="password != null">
and password = #{password}
</if>
</where>
</select>
</mapper>
涓€瀹氫笉瑕佸繕璁板湪 mybatis-config.xml 涓紩鍏?UserMapper.xml
mybatis-config.xml
<mappers>
<!-- 杩欓噷寮曞叆 Mapper xml 鏂囦欢-->
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
4銆佹瀯寤?UserDao
鐩綍锛?/p>
鈹斺攢鈹€ imcoding
鈹溾攢鈹€ App.java
鈹溾攢鈹€ MybatisUtils.java
鈹溾攢鈹€ dao
鈹斺攢鈹€ UserDao.java
UserDao
/**
* Author锛氬瘑鍙?
*/
public class UserDao {
// 鏂板
public int insertUser(User user) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.insertUser(user);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鍒犻櫎
public int deleteUser(int id) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.deleteUser(id);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏇存柊
public int updateUser(User user) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.updateUser(user);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏌ヨ
public User selectUser(int id) {
SqlSession sqlSession = MybatisUtils.getSession();
User user;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? user = mapper.selectUser(id);
sqlSession.commit();//鎻愪氦浜嬪姟
return user;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏍规嵁 username 鍜?password 鏌ヨ
public User selectByUsernameAndPassword(String username, String password) {
SqlSession sqlSession = MybatisUtils.getSession();
User user;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? user = mapper.selectByUsernameAndPassword(username, password);
sqlSession.commit();//鎻愪氦浜嬪姟
return user;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
}
5銆佹祴璇?/h3>
鐩綍锛?/p>
鈹斺攢鈹€ test
鈹斺攢鈹€ java
鈹斺攢鈹€ me
鈹斺攢鈹€ imcoding
鈹斺攢鈹€ AppTest.java
AppTest
/**
* Author锛氬瘑鍙?
*/
public class UserDao {
// 鏂板
public int insertUser(User user) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.insertUser(user);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鍒犻櫎
public int deleteUser(int id) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.deleteUser(id);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏇存柊
public int updateUser(User user) {
SqlSession sqlSession = MybatisUtils.getSession();
int bo;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? bo = mapper.updateUser(user);
sqlSession.commit();//鎻愪氦浜嬪姟
return bo;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏌ヨ
public User selectUser(int id) {
SqlSession sqlSession = MybatisUtils.getSession();
User user;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? user = mapper.selectUser(id);
sqlSession.commit();//鎻愪氦浜嬪姟
return user;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// 鏍规嵁 username 鍜?password 鏌ヨ
public User selectByUsernameAndPassword(String username, String password) {
SqlSession sqlSession = MybatisUtils.getSession();
User user;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? user = mapper.selectByUsernameAndPassword(username, password);
sqlSession.commit();//鎻愪氦浜嬪姟
return user;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
}
澶嶆潅鏌ヨ
鏌ヨ鏌愪釜 user 鐨勮鎯呬笌鑿滃崟鏉冮檺
SQL锛?/p>
SELECT
u.id,
u.username,
u.password,
u.nickname,
ui.name,
ui.sex,
ui.email,
ui.address,
ui.phone,
r.id as role_id,
r.name as role_name,
m.id as menu_id,
m.name as menu_name
FROM
t_user u
LEFT outer JOIN t_user_role ur ON u.id = ur.user_id
LEFT outer JOIN t_role r ON r.id = ur.role_id
left OUTER JOIN t_role_menu rm ON rm.role_id = r.id
left outer join t_menu m on m.id = rm.menu_id
left outer join t_user_info ui on u.id = ui.user_id
WHERE u.username = 鈥?a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9dfd8d7dedff9888f8a97dad6d4">[email protected]
鈥?/code>
1銆佹柊澧炴帴鍙f柟娉?/h3>
UserMapper.java
public interface UserMapper {
//....
// 鏌ヨ user 璇︾粏淇℃伅锛屼互map鏂瑰紡杩斿洖缁撴灉
User selectUserInfo(int id);
//....
}
2銆乁serMapper.xml 鏂板鏌ヨ
UserMapper.xml
<mapper>
.....
<select id="selectUserInfo" resultMap="UserResultMap">
SELECT
u.id,
u.username,
u.password,
u.nickname,
ui.id as ui_id,
ui.name as ui_name,
ui.sex as ui_sex,
ui.email as ui_email,
ui.address as ui_address,
ui.phone as ui_phone,
r.id as role_id,
r.name as role_name,
m.id as menu_id,
m.name as menu_name
FROM
t_user u
LEFT outer JOIN t_user_role ur ON u.id = ur.user_id
LEFT outer JOIN t_role r ON r.id = ur.role_id
left OUTER JOIN t_role_menu rm ON rm.role_id = r.id
left outer join t_menu m on m.id = rm.menu_id
left outer join t_user_info ui on u.id = ui.user_id
WHERE u.id = #{id}
</select>
<!--瀹氫箟杩斿洖缁撴灉鐨勬槧灏勫叧绯?->
<resultMap id="UserResultMap" type="me.imcoding.model.User">
<!--瀵瑰簲 User id 鍙傛暟鏋勯€?->
<constructor>
<idArg column="id" javaType="int" ></idArg>
</constructor>
<!--User 灞炴€?->
<result column="username" property="username"></result>
<result column="password" property="password"></result>
<result column="nickname" property="nickname"></result>
<!--瀵瑰簲User 涓?userInfo 瀵硅薄灞炴€?->
<association property="userInfo" javaType="me.imcoding.model.UserInfo" >
<!--id 寰堥噸瑕佷笉瑕佺渷鐣?->
<id column="ui_id" property="id"></id>
<result column="ui_name" property="name"></result>
<result column="ui_sex" property="sex"></result>
<result column="ui_email" property="email"></result>
<result column="ui_address" property="address"></result>
<result column="ui_phone" property="phone"></result>
</association>
<!--瀵瑰簲 User 鐨?roles 闆嗗悎灞炴€?->
<collection property="roles" ofType="me.imcoding.model.Role">
<id column="role_id" property="id"></id>
<result column="role_name" property="name"></result>
</collection>
<!--瀵瑰簲 User 鐨?menus 闆嗗悎灞炴€?->
<collection property="menus" ofType="me.imcoding.model.Menu">
<id column="menu_id" property="id"></id>
<result column="menu_name" property="name"></result>
</collection>
</resultMap>
....
</mapper>
3銆乁ser 绫绘柊澧炲睘鎬у拰鏋勯€?/h3>
User
public class User {
// ....
// 鏃犲弬鏋勯€? public User() {}
// id 鍙傛暟鏋勯€?@Param("id") 涓巑apper xml 涓殑 name灞炴€т竴鑷? public User(Integer id) {
this.id = id;
}
private UserInfo userInfo;
private List<Role> roles;
private List<Menu> menus;
// ....
}
4銆乁serDao 鏂板鏌ヨ
UserDao
public class UserDao {
//....
// 鏌ヨ鐢ㄦ埛鐨勬墍鏈夎缁嗕俊鎭? public User selectUserInfo(int id) {
SqlSession sqlSession = MybatisUtils.getSession();
User user;
try {
// 浠?sqlSession 涓幏鍙?Mapper 瀹炰緥
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 璋冪敤 mapper 鐨勬柟娉? user = mapper.selectUserInfo(id);
return user;
} finally {
//涓€瀹氳鍏抽棴 sqlSession
sqlSession.close();
}
}
// ....
}
5銆佹祴璇?/h3>
AppTest
public class AppTest {
//......
@Test
public void selectUserInfo() {
UserDao dao = new UserDao();
User user = dao.selectUserInfo(1);
System.out.println(JSON.toJSONString(user,true));
}
//......
}
缁撴灉锛?/p>
{
"id":1,
"menus":[
{
"id":1,
"name":"绔欑偣淇℃伅",
"type":0
},
{
"id":2,
"name":"杞﹁締鏌ヨ",
"type":0
},
{
"id":3,
"name":"浜哄憳绠$悊",
"type":0
},
{
"id":4,
"name":"鐢ㄦ埛绠$悊",
"type":0
},
{
"id":5,
"name":"瑙掕壊绠$悊",
"type":0
},
{
"id":6,
"name":"鑿滃崟绠$悊",
"type":0
},
{
"id":7,
"name":"绔欑偣缁存姢",
"type":0
}
],
"nickname":"鏂规柟",
"password":"111111",
"roles":[
{
"id":1,
"name":"绠$悊鍛?
}
],
"userInfo":{
"address":"涓婃捣闂佃鍖?8",
"email":"[email protected]",
"id":1,
"name":"鏂硅揪",
"phone":"18276546543",
"sex":"鐢?
},
"username":"[email protected]"
}
婧愮爜
Mybatis 鐭ヨ瘑杈圭晫
- Mybatis 寮曞叆
- Mybatis 鐨勫繀瑕侀厤缃?/li>
- Mybatis Mapper XML 锛堟槧灏勬枃浠讹級
- Mybatis 鍔ㄦ€?SQL
- Mybatis SqlSession
- Mybatis 浠g爜鐢熸垚
- Mybatis 缂撳瓨
- Mybatis 浜嬪姟
瀛︿範璧勬簮锛?a href="http://www.mybatis.org/mybatis-3/zh/index.html" rel="nofollow">http://www.mybatis.org/mybatis-3/zh/index.html
Mybatis 鏍稿績鐭ヨ瘑
-
Mapper XML 鏄犲皠鏂囦欢鐨勪娇鐢ㄨ鍒?/p>
瀛︿範锛?a href="http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html" rel="nofollow">http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html
-
鍔ㄦ€?SQL 鐨勫鐞嗚鍒?/p>
瀛︿範锛?a href="http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html" rel="nofollow">http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html
以上是关于Mybatis 浠g爜瀹炶返的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯