本文为大家分享了mybatis入门学习资料,供大家参考,具体内容如下
所需要用到的其他工具或技术:
项目管理工具 : Maven
测试运行工具 : Junit
数据库 : Derby
废话不多说,直接代码
Maven Dependencies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version> 3.2 . 7 </version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.9 </version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version> 10.10 . 2.0 </version> </dependency> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version> 10.10 . 2.0 </version> </dependency> </dependencies> |
SQL 建表及数据插入:
1
2
3
4
5
6
7
8
9
10
|
CREATE TABLE USER_TEST_TB( ID INT PRIMARY KEY , USERNAME VARCHAR (20) NOT NULL , PASSWORD VARCHAR (20) NOT NULL , NICKNAME VARCHAR (20) NOT NULL ); INSERT INTO USER_TEST_TB VALUES (1, '1st' , '111' , 'Jack' ); INSERT INTO USER_TEST_TB VALUES (2, '2nd' , '222' , 'Rose' ); INSERT INTO USER_TEST_TB VALUES (3, '3rd' , '333' , 'Will' ); |
Mybatis配置文件 src/main/resource源目录下
test-mybatis-configuration.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? 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 > < property name = "driver" value = "org.apache.derby.jdbc.ClientDriver" /> < property name = "url" value = "jdbc:derby://localhost:1527/freud;create=true" /> </ properties > < environments default = "development" > < environment id = "development" > < transactionManager type = "JDBC" /> < dataSource type = "POOLED" > < property name = "driver" value = "${driver}" /> < property name = "url" value = "${url}" /> </ dataSource > </ environment > </ environments > < mappers > <!-- <mapper class="com.freud.practice.UserMapper" /> --> < mapper resource = "com/freud/practice/UserMapper.xml" /> </ mappers > </ configuration > |
UserMapper.xml Mapper文件(src/main/java/com.freud.practice目录下)
1
2
3
4
5
6
7
8
9
10
|
<? 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.freud.practice.UserMapper" > < select id = "getUser" resultType = "com.freud.practice.User" > select * from USER_TEST_TB </ select > </ mapper > |
User.Java对象类(src/main/java/com.freud.practice目录下)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.freud.practice; public class User { private Integer id; private String username; private String password; private String nickname; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this .nickname = nickname; } } |
UserMapper.java Mapper类(src/main/java/com.freud.practice目录下)
1
2
3
4
5
6
7
8
9
10
|
package com.freud.practice; import java.util.List; import org.apache.ibatis.annotations.Select; public interface UserMapper { //@Select("SELECT * FROM USER_TEST_TB") public List<User> getUser(); } |
测试类TestMyBatis.java(src/test/java/com.freud.practice目录下)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package com.freud.practice; import java.io.InputStream; import java.text.MessageFormat; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; public class TestMyBatis { private String source; private InputStream inputStream; private SqlSessionFactory sqlSessionFactory; @Before public void setUp() { source = "test-mybatis-configuration.xml" ; } @Test public void testXMLConfingureSessionFactory() { try { inputStream = TestMyBatis. class .getClassLoader().getResourceAsStream(source); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); List<User> users = (List) session.selectList( "com.freud.practice.UserMapper.getUser" ); System.out.println( "Query by XML configuration..." ); this .printUsers(users); UserMapper userMapper = session.getMapper(UserMapper. class ); System.out.println( "\r\nQuery by annotation configuration..." ); this .printUsers(userMapper.getUser()); } catch (Exception e) { e.printStackTrace(); } } private void printUsers( final List<User> users) { int count = 0 ; for (User user : users) { System.out.println(MessageFormat.format( "==User[{0}]=================" , ++count)); System.out.println( "User Id: " + user.getId()); System.out.println( "User UserName: " + user.getUsername()); System.out.println( "User Password: " + user.getPassword()); System.out.println( "User nickname: " + user.getNickname()); } } } |
以上是使用XML作为Mapper配置文件的一个入门,其实Mybatis还支持Annotation的方式,具体操作如下:
1. 打开UserMapper.java中的注释。
2. 打开test-mybatis-configuration.xml中的注释,并注释掉现有的Mapper
3. 删除UserMapper.xml文件
这种是Mybatis的Annotation方式的Mapper配置。个人比较偏向于XML方式,所以后续的教程会更倾向于XML配置的方式来写。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。