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>
<!--
拿到数据库信息,这是db.properties的内容
driver=com.MySQL.jdbc.Driver
url=jdbc:mysql: //localhost:3306/usertest
username=root
password=root
--!>
<properties resource= "db.properties" >
</properties>
<!-- 别名优化--!>
<typeAliases>
<!-- < package name= "com.leige.entity" /> -->
<typeAlias type= "com.leige.entity.User" alias= "user" />
</typeAliases>
<!--设置框架工作模式development开发者模式 ,work是工作模式 -->
<environments default = "development" >
<environment id= "development" >
<transactionManager type= "jdbc" ></transactionManager>
<!-- 配置数据源 -->
<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>
<!-- 使用xml,mapper resource需要设置--!>
<!-- <mappers>
<mapper resource= "com/leige/entity/UserMappen.xml" ></mapper>
</mappers> -->
<!--使用注解,mapper配置 class ,查询方法写在接口中--!>
<mappers>
<mapper class = "com.leige.impl.UserImpl" />
</mappers>
|
使用xml需要再配置一个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" >
<!-- 建立表与类的映射关系有两种:一个是xml(倾向),另一个是注解 -->
<mapper namespace= "com.leige.entity.UserMappen" >
<!-- 查询全部 -->
<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();
}
//查询全部
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开发环境搭建实现数据的增删改查功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://blog.csdn.net/leigelg/article/details/59638687