图片] 映射文件.jpg
![](http://static.oschina.net/uploads/code/201102/22145435_lUp4.jpg)
[图片] UserService.jpg
![](http://static.oschina.net/uploads/code/201102/22145435_kEmX.jpg)
[图片] spring配置文件.jpg
![](http://static.oschina.net/uploads/code/201102/22145435_PW1a.jpg)
[图片] mybatis_config.jpg
![](http://static.oschina.net/uploads/code/201102/22145435_Zrkw.jpg)
[图片] MBS.jpg
![](http://static.oschina.net/uploads/code/201102/22145517_jEEc.jpg)
[图片] IUserService.jpg
![](http://static.oschina.net/uploads/code/201102/22145517_OBdr.jpg)
[图片] IUserMapper.jpg
![](http://static.oschina.net/uploads/code/201102/22145517_ZKm5.jpg)
[代码] [Java]代码
001 | 映射文件内容(User.xml) |
002 | <?xml version= "1.0" encoding= "UTF-8" ?> |
003 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > |
004 | <mapper namespace= "mbs.com.mapper.IUserMapper" > |
005 | <resultMap type= "User" id= "uM" > |
006 | <id property= "id" column= "id" /> |
007 | <result property= "address" column= "address" /> |
008 | <result property= "birthday" column= "birthday" /> |
009 | <result property= "cardId" column= "cardId" /> |
010 | <result property= "email" column= "email" /> |
011 | <result property= "loginName" column= "loginName" /> |
012 | <result property= "passWord" column= "passWord" /> |
013 | <result property= "pictureUrl" column= "pictureUrl" /> |
014 | <result property= "sex" column= "sex" /> |
015 | <result property= "telphone" column= "telphone" /> |
016 | <result property= "userName" column= "userName" /> |
017 | </resultMap> |
018 | |
019 | <!--查询:通过用户ID返回一个用户对象 --> |
020 | <select id= "getUser" resultMap= "uM" parameterType= "Integer" > |
021 | select * from users |
022 | <where>#{id}</where> |
023 | </select> |
024 | </mapper> |
025 | |
026 | mybatis_config.xml内容 |
027 | |
028 | <?xml version= "1.0" encoding= "UTF-8" ?> |
029 | <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > |
030 | <configuration> |
031 | <typeAliases> |
032 | <typeAlias alias= "user" type= "mbs.com.pojo.User" /> |
033 | </typeAliases> |
034 | <mappers> |
035 | <mapper resource= "mapper_sql/User.xml" /> |
036 | </mappers> |
037 | </configuration> |
038 | |
039 | IUserService.java 接口内容 |
040 | public interface IUserService |
041 | { |
042 | User getUser(Integer id); |
043 | } |
044 | |
045 | IUserMapper.java 映射器内容 |
046 | |
047 | @Mapper ( "userMapper" ) |
048 | public interface IUserMapper |
049 | { |
050 | User getUser(Integer id); |
051 | } |
052 | |
053 | FooService.java 实现类内容 |
054 | @Service ( "fooService" ) |
055 | public class FooService implements IUserService |
056 | { |
057 | @Autowired |
058 | private IUserMapper userMapper; |
059 | |
060 | public User getUser(Integer id) |
061 | { |
062 | return userMapper.getUser(id); |
063 | } |
064 | |
065 | } |
066 | |
067 | |
068 | |
069 | spring.xm内容 |
070 | <?xml version= "1.0" encoding= "UTF-8" ?> |
071 | <beans xmlns= "http://www.springframework.org/schema/beans" |
072 | xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" |
073 | xmlns:p= "http://www.springframework.org/schema/p" |
074 | xmlns:tx= "http://www.springframework.org/schema/tx" |
075 | xmlns:aop= "http://www.springframework.org/schema/aop" |
076 | xmlns:context= "http://www.springframework.org/schema/context" |
077 | xsi:schemaLocation="http: //www.springframework.org/schema/beans |
078 | http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd |
079 | http: //www.springframework.org/schema/tx |
080 | http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd |
081 | http: //www.springframework.org/schema/aop |
082 | http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd |
083 | http: //www.springframework.org/schema/context |
084 | http: //www.springframework.org/schema/context/spring-context-2.5.xsd" |
085 | default -init-method= "init" > |
086 | <!-- Annotation Config --> |
087 | <context:annotation-config/> |
088 | |
089 | <!--注册数据库的连接信息 --> |
090 | <context:property-placeholder location= "classpath:config/jdbc.properties" /> |
091 | |
092 | <!-- 扫描物理路径及注册 --> |
093 | <context:component-scan base- package = "mbs.com" /> |
094 | |
095 | <!-- Data Source --> |
096 | <bean id= "dataSource" class = "org.logicalcobwebs.proxool.ProxoolDataSource" > |
097 | <property name= "driver" value= "${dirver}" /> |
098 | <property name= "driverUrl" value= "${url}" /> |
099 | <property name= "user" value= "${username}" /> |
100 | <property name= "password" value= "${password}" /> |
101 | <property name= "minimumConnectionCount" value= "${min_conn_count}" /> |
102 | <property name= "maximumConnectionCount" value= "${max_conn_count}" /> |
103 | </bean> |
104 | |
105 | <!-- 配置mybatis的sqlsessionFactory --> |
106 | <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > |
107 | <property name= "dataSource" ref= "dataSource" /> |
108 | <property name= "configLocation" value= "classpath:mybatis/mybatis_config.xml" /> |
109 | </bean> |
110 | |
111 | <!-- Transaction Manager --> |
112 | <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > |
113 | <property name= "dataSource" ref= "dataSource" /> |
114 | </bean> |
115 | |
116 | <!--配置 ibatis的映射器 --> |
117 | <bean id= "userMapper" class = "org.mybatis.spring.MapperFactoryBean" > |
118 | <property name= "sqlSessionFactory" ref= "sqlSessionFactory" /> |
119 | <property name= "mapperInterface" value= "mbs.com.mapper.IUserMapper" /> |
120 | </bean> |
121 | </beans> |
122 | |
123 | main 函数测试 |
124 | public static void main(String[] args) |
125 | { |
126 | ApplicationContext context = new ClassPathXmlApplicationContext( "spring.xml" ); |
127 | IUserService service =(IUserService)context.getBean( "fooService" ); |
128 | User user = service.getUser( 1 ); |
129 | System.out.println( "用户名:" +user.getUserName()); |
130 | System.out.println( "密 码:" +user.getPassWord()); |
131 | } |