mybatis3.0.2整合spring3.0

mybatis3.0.2整合spring3.0

周丰达 发布于 2011年02月22日 14时,  13评/14662阅
分享到: 
收藏 +57
0
<无详细内容>
标签:  Spring  MyBatis

代码片段(8)[全屏查看所有代码]

1. [图片] 映射文件.jpg    

2. [图片] UserService.jpg    

3. [图片] spring配置文件.jpg    

4. [图片] mybatis_config.jpg    

5. [图片] MBS.jpg    

6. [图片] IUserService.jpg    

7. [图片] IUserMapper.jpg    

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在线编辑演示 二维码 更多»

发表评论 回到顶部网友评论(13)

  • 1楼: azheng 发表于 2011-02-28 20:41  回复此评论
    mybatis 3.0.5 + mybatis-spring 1.0.1 + spring 3.0.5 整合成功的路过
  • 2楼: 龙影 发表于 2011-03-01 10:23  回复此评论
    我感觉mybatis也不怎么样啊 配置文件太多 容易乱。 
    对于动态sql的组建不是很好 还是遗留了ibatis的毛病。 

    大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢 
    http://www.oschina.net/code/snippet_105457_2453   


  • 3楼: soltex 发表于 2011-06-23 10:12  回复此评论
    @Mapper 这个annotation 是 在 Mybatis 的包下的么 ?怎么没发现呢? 
  • 4楼: |.baby.| 发表于 2011-07-14 10:26  回复此评论
    引用来自“龙影”的评论
    我感觉mybatis也不怎么样啊 配置文件太多 容易乱。 
    对于动态sql的组建不是很好 还是遗留了ibatis的毛病。 

    大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢 
    http://www.oschina.net/code/snippet_105457_2453   


    看了你写的,代码逻辑比较乱,有点自造车轮的感觉
    另外,LZ你的配置文件写的也不是那么优雅,其实配置好的话后面的开发需要写的配置文件就只有Mapper的配置文件了,并没有这么繁琐
  • 5楼: 河南_程飞 发表于 2011-08-17 16:33  回复此评论
    引用来自“|.baby.|”的评论
    引用来自“龙影”的评论
    我感觉mybatis也不怎么样啊 配置文件太多 容易乱。 
    对于动态sql的组建不是很好 还是遗留了ibatis的毛病。 

    大家可以参考下我写的 可能不足的地方希望多多指出 以便完善我的代码 谢谢 
    http://www.oschina.net/code/snippet_105457_2453   


    看了你写的,代码逻辑比较乱,有点自造车轮的感觉
    另外,LZ你的配置文件写的也不是那么优雅,其实配置好的话后面的开发需要写的配置文件就只有Mapper的配置文件了,并没有这么繁琐
    ls有没有规范一些的demo。
    我照lz的代码整合成功了,不过确实觉得不够优雅。
  • 6楼: puras 发表于 2012-03-30 15:55  回复此评论
    都MyBatis了。。。。还 
    ?
    1
    2
    3
    < mappers >
         < mapper resource = "mapper_sql/User.xml" />   
    </ mappers >

    LZ你确认你没在拿MyBatis当iBatis用么? 

  • 7楼: 周丰达 发表于 2012-03-30 22:43  回复此评论
    引用来自“puras”的评论
    都MyBatis了。。。。还 
    ?
    1
    2
    3
    < mappers >
         < mapper resource = "mapper_sql/User.xml" />   
    </ mappers >

    LZ你确认你没在拿MyBatis当iBatis用么? 

    高人指路
  • 8楼: puras 发表于 2012-03-31 10:08  回复此评论
    引用来自“周丰达”的评论
    引用来自“puras”的评论
    都MyBatis了。。。。还 
    ?
    1
    2
    3
    < mappers >
         < mapper resource = "mapper_sql/User.xml" />   
    </ mappers >

    LZ你确认你没在拿MyBatis当iBatis用么? 

    高人指路
    我也加了一段代码。可以参考: http://www.oschina.net/code/snippet_96921_9662
  • 9楼: MR_xing 发表于 2012-11-04 17:07  回复此评论

    LZ  貌似这里错了吧?
    005    <resultMap type="User" id="uM">

    032        <typeAlias alias="user" 
    别名不一样  ,shi 
  • 10楼: skyone 发表于 2013-02-25 09:52  回复此评论
    关于spring3+struts2+mybatis3 整合,能否获取表结构信息,我知道jdbc中使用getMetaData()方法获取表结构信息。 
    如果mybatis3可以获取表结构信息,代码怎么写呢? 
    求指教。。。。
  • 11楼: zp-wmhx 发表于 2014-04-28 16:37  回复此评论
    引用来自“skyone”的评论
    关于spring3+struts2+mybatis3 整合,能否获取表结构信息,我知道jdbc中使用getMetaData()方法获取表结构信息。 
    如果mybatis3可以获取表结构信息,代码怎么写呢? 
    求指教。。。。
    看源码.
  • 12楼: lishaorui 发表于 2014-07-29 11:33  回复此评论
      mybatis-spring,spring配置文件里加上这个 
    <context:annotation-config /> 

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
            <property name="basePackage" value="com.mapperdir" /> 
    </bean> 
    然后对应文件夹下,写好XXXMapper接口和XXXMapper.xml就可以了
  • 13楼: moluq 发表于 2014-12-04 22:44  回复此评论
    引用来自“lishaorui”的评论
      mybatis-spring,spring配置文件里加上这个 
    <context:annotation-config /> 

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
            <property name="basePackage" value="com.mapperdir" /> 
    </bean> 
    然后对应文件夹下,写好XXXMapper接口和XXXMapper.xml就可以了
    正解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值