config.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?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> <!-- 拿到數(shù)據(jù)庫信息,這是db.properties的內(nèi)容 driver=com.MySQL.jdbc.Driver url=jdbc:mysql: //localhost:3306/usertest username=root password=root --!> <properties resource= "db.properties" > </properties> <!-- 別名優(yōu)化--!> <typeAliases> <!-- < package name= "com.leige.entity" /> --> <typeAlias type= "com.leige.entity.User" alias= "user" /> </typeAliases> <!--設(shè)置框架工作模式development開發(fā)者模式 ,work是工作模式 --> <environments default = "development" > <environment id= "development" > <transactionManager type= "jdbc" ></transactionManager> <!-- 配置數(shù)據(jù)源 --> <dataSource type= "POOLED" > <!-- 注冊驅(qū)動 --> <property name= "driver" value= "${driver}" /> <!-- 連接數(shù)據(jù)庫 --> <property name= "url" value= "${url}" /> <!-- 密碼帳號登錄 --> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> </dataSource> </environment> </environments> <!-- 使用xml,mapper resource需要設(shè)置--!> <!-- <mappers> <mapper resource= "com/leige/entity/UserMappen.xml" ></mapper> </mappers> --> <!--使用注解,mapper配置 class ,查詢方法寫在接口中--!> <mappers> <mapper class = "com.leige.impl.UserImpl" /> </mappers> |
使用xml需要再配置一個(gè)mappen.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
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
66
67
|
<?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" > <!-- 建立表與類的映射關(guān)系有兩種:一個(gè)是xml(傾向),另一個(gè)是注解 --> <mapper namespace= "com.leige.entity.UserMappen" > <!-- 查詢?nèi)?--> <select id= "selectAll" resultType= "user" > select * from user </select> <!-- 增加用戶 --> <update id= "insertUser" parameterType= "user" > insert into user values ( null ,#{name},#{password},#{age}) </update> </mapper> 使用注解則用接口 public interface UserImpl { //更新用戶,更新用@update,查詢用@select,插入用@insert,刪除使用@delete這些注解 @Update ( "update user set name=#{name} where id=#{id}" ) public void updateUser(User user); } demo測試代碼 public class Demo { static InputStream in = null ; static SqlSessionFactory ssf = null ; static { in = Demo. class .getClassLoader().getResourceAsStream( "config.xml" ); ssf = new SqlSessionFactoryBuilder().build(in); } public static void main(String[] args) { //selectAll(); //insertUser(); updateUser(); } //查詢?nèi)?/code> public static void selectAll(){ SqlSession session = ssf.openSession(); List<User> list = session.selectList( "com.leige.entity.UserMappen.selectAll" ); System.out.println(list); session.close(); } //增加用戶 public static void insertUser(){ SqlSession session = ssf.openSession(); User user = new User(); user.setName( "小霸王" ); user.setPassword( "aaaaa" ); user.setAge( 25 ); int num = session.insert( "com.leige.entity.UserMappen.insertUser" ,user); session.commit(); System.out.println(num); session.close(); } //修改用戶信息 public static void updateUser(){ SqlSession session = ssf.openSession(); UserImpl userImpl = session.getMapper(UserImpl. class ); User user = new User(); user.setName( "我是大頭鬼" ); user.setPassword( "aaaaa" ); user.setAge( 25 ); user.setId( 79 ); userImpl.updateUser(user); session.commit(); session.close(); } } </configuration> |
以上所述是小編給大家介紹的Mybatis開發(fā)環(huán)境搭建實(shí)現(xiàn)數(shù)據(jù)的增刪改查功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/leigelg/article/details/59638687