mybatis3.0.2整合spring3.0
周丰达 发布于 2011年02月22日 14时,
13评/14662阅
代码片段(8)[全屏查看所有代码]
8. [代码][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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
映射文件内容(User.xml)
<?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=
"mbs.com.mapper.IUserMapper"
>
<resultMap type=
"User"
id=
"uM"
>
<id property=
"id"
column=
"id"
/>
<result property=
"address"
column=
"address"
/>
<result property=
"birthday"
column=
"birthday"
/>
<result property=
"cardId"
column=
"cardId"
/>
<result property=
"email"
column=
"email"
/>
<result property=
"loginName"
column=
"loginName"
/>
<result property=
"passWord"
column=
"passWord"
/>
<result property=
"pictureUrl"
column=
"pictureUrl"
/>
<result property=
"sex"
column=
"sex"
/>
<result property=
"telphone"
column=
"telphone"
/>
<result property=
"userName"
column=
"userName"
/>
</resultMap>
<!--查询:通过用户ID返回一个用户对象 -->
<select id=
"getUser"
resultMap=
"uM"
parameterType=
"Integer"
>
select * from users
<where>#{id}</where>
</select>
</mapper>
mybatis_config.xml内容
<?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>
<typeAliases>
<typeAlias alias=
"user"
type=
"mbs.com.pojo.User"
/>
</typeAliases>
<mappers>
<mapper resource=
"mapper_sql/User.xml"
/>
</mappers>
</configuration>
IUserService.java 接口内容
public
interface
IUserService
{
User getUser(Integer id);
}
IUserMapper.java 映射器内容
@Mapper
(
"userMapper"
)
public
interface
IUserMapper
{
User getUser(Integer id);
}
FooService.java 实现类内容
@Service
(
"fooService"
)
public
class
FooService
implements
IUserService
{
@Autowired
private
IUserMapper userMapper;
public
User getUser(Integer id)
{
return
userMapper.getUser(id);
}
}
spring.xm内容
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:aop=
"http://www.springframework.org/schema/aop"
xmlns:context=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd"
default
-init-method=
"init"
>
<!-- Annotation Config -->
<context:annotation-config/>
<!--注册数据库的连接信息 -->
<context:property-placeholder location=
"classpath:config/jdbc.properties"
/>
<!-- 扫描物理路径及注册 -->
<context:component-scan base-
package
=
"mbs.com"
/>
<!-- Data Source -->
<bean id=
"dataSource"
class
=
"org.logicalcobwebs.proxool.ProxoolDataSource"
>
<property name=
"driver"
value=
"${dirver}"
/>
<property name=
"driverUrl"
value=
"${url}"
/>
<property name=
"user"
value=
"${username}"
/>
<property name=
"password"
value=
"${password}"
/>
<property name=
"minimumConnectionCount"
value=
"${min_conn_count}"
/>
<property name=
"maximumConnectionCount"
value=
"${max_conn_count}"
/>
</bean>
<!-- 配置mybatis的sqlsessionFactory -->
<bean id=
"sqlSessionFactory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
<property name=
"configLocation"
value=
"classpath:mybatis/mybatis_config.xml"
/>
</bean>
<!-- Transaction Manager -->
<bean id=
"transactionManager"
class
=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
</bean>
<!--配置 ibatis的映射器 -->
<bean id=
"userMapper"
class
=
"org.mybatis.spring.MapperFactoryBean"
>
<property name=
"sqlSessionFactory"
ref=
"sqlSessionFactory"
/>
<property name=
"mapperInterface"
value=
"mbs.com.mapper.IUserMapper"
/>
</bean>
</beans>
main 函数测试
public
static
void
main(String[] args)
{
ApplicationContext context =
new
ClassPathXmlApplicationContext(
"spring.xml"
);
IUserService service =(IUserService)context.getBean(
"fooService"
);
User user = service.getUser(
1
);
System.out.println(
"用户名:"
+user.getUserName());
System.out.println(
"密 码:"
+user.getPassWord());
}
|
开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»
对于动态sql的组建不是很好 还是遗留了ibatis的毛病。
大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢
http://www.oschina.net/code/snippet_105457_2453
引用来自“龙影”的评论
对于动态sql的组建不是很好 还是遗留了ibatis的毛病。
大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢
http://www.oschina.net/code/snippet_105457_2453
另外,LZ你的配置文件写的也不是那么优雅,其实配置好的话后面的开发需要写的配置文件就只有Mapper的配置文件了,并没有这么繁琐
引用来自“|.baby.|”的评论
引用来自“龙影”的评论
对于动态sql的组建不是很好 还是遗留了ibatis的毛病。
大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢
http://www.oschina.net/code/snippet_105457_2453
另外,LZ你的配置文件写的也不是那么优雅,其实配置好的话后面的开发需要写的配置文件就只有Mapper的配置文件了,并没有这么繁琐
我照lz的代码整合成功了,不过确实觉得不够优雅。
<
mappers
>
<
mapper
resource
=
"mapper_sql/User.xml"
/>
</
mappers
>
引用来自“puras”的评论
<
mappers
>
<
mapper
resource
=
"mapper_sql/User.xml"
/>
</
mappers
>
引用来自“周丰达”的评论
引用来自“puras”的评论
<
mappers
>
<
mapper
resource
=
"mapper_sql/User.xml"
/>
</
mappers
>
LZ 貌似这里错了吧?
005
<resultMap type=
"User"
id=
"uM"
>
032
<typeAlias alias=
"user"
如果mybatis3可以获取表结构信息,代码怎么写呢?
求指教。。。。
引用来自“skyone”的评论
关于spring3+struts2+mybatis3 整合,能否获取表结构信息,我知道jdbc中使用getMetaData()方法获取表结构信息。如果mybatis3可以获取表结构信息,代码怎么写呢?
求指教。。。。
<context:annotation-config />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapperdir" />
</bean>
然后对应文件夹下,写好XXXMapper接口和XXXMapper.xml就可以了
引用来自“lishaorui”的评论
用 mybatis-spring,spring配置文件里加上这个<context:annotation-config />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapperdir" />
</bean>
然后对应文件夹下,写好XXXMapper接口和XXXMapper.xml就可以了