Mybatis简介大家可以自行google之。官网为:http://code.google.com/p/mybatis/
我也刚刚开始初学,先来一个小例子:
创建一个User.java文件,代码如下:
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
|
package
com.rollen;
public
class
User {
private
String name;
private
int
age;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
@Override
public
String toString() {
return
"Name:"
+name+
"Age"
+age;
}
}
|
上面的java文件位于com.rollen包中,然后在这个包中建立generator.xml文件,代码为:
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
configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
<
configuration
>
<
typeAliases
>
<
typeAlias
alias="User" type="com.rollen.User" />
</
typeAliases
>
<
environments
default="development">
<
environment
id="development">
<
transactionManager
type="JDBC" />
<
dataSource
type="POOLED">
<
property
name="driver" value="com.mysql.jdbc.Driver" />
<
property
name="username" value="root" />
<
property
name="password" value="root" />
</
dataSource
>
</
environment
>
</
environments
>
<
mappers
>
<
mapper
resource="com/rollen/user.xml" />
</
mappers
>
</
configuration
>
|
在com.rollen包下面创建user.xml文件,代码为:
1
2
3
4
5
6
7
8
|
<?
xml
version="1.0" encoding="UTF-8" ?>
<!
DOCTYPE
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<
mapper
namespace="com.rollen.User">
<
select
id="selectUser" parameterType="int" resultType="User" >
select * from user where age = #{age}
</
select
>
</
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
|
package
com.rollen;
import
java.io.*;
import
org.apache.ibatis.io.Resources;
import
org.apache.ibatis.session.SqlSession;
import
org.apache.ibatis.session.SqlSessionFactory;
import
org.apache.ibatis.session.SqlSessionFactoryBuilder;
public
class
Test {
public
static
void
main(String[] args) {
String resource=
"com/rollen/generatorConfig.xml"
;
Reader reader =
null
;
try
{
reader = Resources.getResourceAsReader(resource);
}
catch
(IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory=
new
SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
try
{
User user=sqlSession.selectOne(
"selectUser"
,
10
);
System.out.println(user);
}
finally
{
sqlSession.close();
}
}
}
|
在上面的generateConfig.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
|
<?
xml
version="1.0" encoding="UTF-8" ?>
<!
DOCTYPE
configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
<
configuration
>
<
properties
>
<
property
name="username" value="root" />
</
properties
>
<
typeAliases
>
<
typeAlias
alias="User" type="com.rollen.User" />
</
typeAliases
>
<
environments
default="development">
<
environment
id="development">
<
transactionManager
type="JDBC" />
<
dataSource
type="POOLED">
<
property
name="driver" value="com.mysql.jdbc.Driver" />
<
property
name="username" value="${username}" />
<
property
name="password" value="root" />
</
dataSource
>
</
environment
>
</
environments
>
<
mappers
>
<
mapper
resource="com/rollen/user.xml" />
</
mappers
>
</
configuration
>
|
注意,和上面的区别是我们添加了:
1
2
3
|
<
properties
>
<
property
name="username" value="root" />
</
properties
>
|
并且修改了这行代码:
1
|
<
property
name="username" value="${username}" />
|
还可以有如下的修改方法,新建一个config.properties文件,文件内容为:
1
|
password=root
|
然后将generatorconfig.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
|
<?
xml
version="1.0" encoding="UTF-8" ?>
<!
DOCTYPE
configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
<
configuration
>
<
properties
resource="com/rollen/config.properties">
<
property
name="username" value="root" />
</
properties
>
<
typeAliases
>
<
typeAlias
alias="User" type="com.rollen.User" />
</
typeAliases
>
<
environments
default="development">
<
environment
id="development">
<
transactionManager
type="JDBC" />
<
dataSource
type="POOLED">
<
property
name="driver" value="com.mysql.jdbc.Driver" />
<
property
name="username" value="${username}" />
<
property
name="password" value="${password}" />
</
dataSource
>
</
environment
>
</
environments
>
<
mappers
>
<
mapper
resource="com/rollen/user.xml" />
</
mappers
>
</
configuration
>
|
需要注意的是如下的两行代码:
1
2
3
|
<
properties
resource="com/rollen/config.properties">
....
<
property
name="password" value="${password}" />
|