标签:
1
2
3
4
|
log4j.rootLogger=DEBUG, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" < configuration > <!-- 和spring整合后environments配置将废除 --> < environments default = "development" > < environment id = "development" > < transactionManager type = "JDBC" /> < dataSource type = "POOLED" > < property name = "driver" value = "com.mysql.jdbc.Driver" /> < property name = "url" value = "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8" /> < property name = "username" value = "root" /> < property name = "password" value = "root" /> </ dataSource > </ environment > </ environments > </ configuration > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.hqu.model; import java.sql.Date; public class User { private int id; private String username; private String sex; private Date birthday; private String address; private String detail; private float score; // getter and setter .. } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" < mapper namespace = "test" > <!-- 查询用户信息 select :将select标签内容称为statement id:是在user.xml的唯一标识,statement的id parmeterType:指向sql(向statement中)中传入参数的类型 #{}:表示一个点位符,{}中的id表示传入变量名,当传入单个值{}中的变量可以随意 resultType:将sql查询结果集映射成java对象的类型 --> < select id = "findUserByName" parameterType = "string" resultType = "com.hqu.model.User" > select * from User where username = #{id} </ select > < insert id = "insertUser" parameterType = "com.hqu.model.User" > insert into user(username,birthday,sex,address,detail,score) values (#{username},#{birthday},#{sex},#{address},#{detail},#{score}) </ insert > </ mapper > |
1
2
3
4
5
6
7
|
< configuration > ... <!-- 加载映射文件 --> < mappers > < mapper resource = "sqlmap/User.xml" /> </ mappers > </ configuration > |
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
|
public class MybatisTest { SqlSession sqlSession = null ; @Before public void before() throws IOException { String resource = "SqlMapConfig.xml" ; // mybatis的配置文件 InputStream is = Resources.getResourceAsStream(resource); // 创建会话工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); sqlSession = sqlSessionFactory.openSession(); // 从会话工厂中的到会话 } @After public void after() { sqlSession.close(); // 释放资源 } @Test public void testSelect() { // 通过sqlSession操作数据库 // 第一个参数:user.xml定义的statement的id // 第二个参数:输入参数 User user = sqlSession.selectOne( "test.findUserByName" , "jerome" ); System.out.println(user); } @Test public void testInsert() { User user = new User(); user.setUsername( "jelly" ); user.setSex( "女" ); user.setAddress( "深圳" ); user.setScore(12f); sqlSession.insert( "test.insertUser" , user); sqlSession.commit(); // 提交事务 } } |
标签:
原文地址:http://blog.csdn.net/jerome_s/article/details/46340043