1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
use bbs; Drop TABLE IF EXISTS `article`; Create TABLE `article` ( `id` int ( 11 ) NOT NULL auto_increment, `userid` int ( 11 ) NOT NULL, `title` varchar( 100 ) NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT= 1 DEFAULT CHARSET=utf8; -- ---------------------------- -- 添加几条测试数据 -- ---------------------------- Insert INTO `article` VALUES ( '1' , '1' , 'test_title' , 'test_content' ); Insert INTO `article` VALUES ( '2' , '1' , 'test_title_2' , 'test_content_2' ); Insert INTO `article` VALUES ( '3' , '1' , 'test_title_3' , 'test_content_3' ); Insert INTO `article` VALUES ( '4' , '1' , 'test_title_4' , 'test_content_4' ); select * from article; create table user( id int primary key, userName varchar( 50 ), userAddress varchar( 50 ), userAge int ( 3 ) )ENGINE=InnoDB AUTO_INCREMENT= 1 DEFAULT CHARSET=utf8; insert into user values( 1 , 'ppppp' , 'aaaaaa' , 20 ); commit; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
package com.mscncn.batis.model; public class User { private int id; private int age; private String userName; private String userAddress; public int getId() { return id; } public void setId( int id) { this .id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this .userName = userName; } public String getUserAddress() { return userAddress; } public void setUserAddress(String userAddress) { this .userAddress = userAddress; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
package com.mscncn.batis.model; public class Article { private int id; private User user; private String title; private String content; public int getId() { return id; } public void setId( int id) { this .id = id; } public User getUser() { return user; } public void setUser(User user) { this .user = user; } public String getTitle() { return title; } public void setTitle(String title) { this .title = title; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } @Override public String toString() { return "Article [id=" + id + ", user=" + user + ", title=" + title + ", content=" + content + "]" ; } } |
1
2
3
4
5
6
7
8
9 |
<typeAliases> <!-- 实体类的别名 --> <!-- 注解使用 package 来指定包, --> <!-- < package name= "com.mscncn.batis.model" />--> <typeAlias alias= "Posts" type= "com.mscncn.batis.model.Posts" /> <typeAlias alias= "Pager" type= "com.mscncn.batis.model.Pager" /> <typeAlias alias= "Article" type= "com.mscncn.batis.model.Article" /> <typeAlias alias= "User" type= "com.mscncn.batis.model.User" /> </typeAliases> |
1
2
3
4
5 |
<mappers> <!-- --> <mapper resource= "com/mscncn/batis/mapper/config/PostsMapper.xml" /> <mapper resource= "com/mscncn/batis/mapper/config/UserMapper.xml" /> </mappers> |
1
2
3 |
public interface UserMapper { public List<Article> getUserArticles( int id); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
<?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必须是PostsMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”--> <mapper namespace= "com.mscncn.batis.mapper.UserMapper" > <select id= "getUserArticles" parameterType= "int" resultMap= "resultUserArticleList" > select user.id,user.userName,user.userAddress,article.id a_id,article.title,article.content from user,article where user.id=article.userid and user.id=#{id} </select> <resultMap type= "Article" id= "resultUserArticleList" > <id property= "id" column= "a_id" /> <result property= "title" column= "title" /> <result property= "content" column= "content" /> <association property= "user" javaType= "User" > <id property= "id" column= "id" /> <result property= "userName" column= "userName" /> <result property= "userAddress" column= "userAddress" /> </association> </resultMap> </mapper> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 |
package com.mscncn.batis; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import com.mscncn.batis.mapper.UserMapper; import com.mscncn.batis.model.Article; import com.mscncn.batis.util.MyBatisUtil; public class UserMapperTest { static SqlSessionFactory sqlSessionFactory = null ; static { sqlSessionFactory = MyBatisUtil.getSqlSessionFactory(); } @Test public void testAdd() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper mapper = sqlSession.getMapper(UserMapper. class ); List<Article> list=mapper.getUserArticles( 1 ); for (Article a:list){ System.out.println(a); } sqlSession.commit(); //这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } } |