Mybatis基於註解的方式訪問資料庫

消失的風發表於2015-07-03

1. 使用方式:在Service層直接呼叫

 1 package com.disappearwind.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Repository;
 5 import org.springframework.stereotype.Service;
 6 
 7 import com.disappearwind.mapper.UserInfoMapper;
 8 import com.disappearwind.model.UserInfo;
 9 
10 
11 /**
12  * 使用者service
13  *
14  */
15 @Service
16 @Repository
17 public class UserInfoService{
18     
19     @Autowired
20     private UserInfoMapper userInfoMapper;
21 
22      public UserInfo selectByPrimaryKey(Integer id){
23          return userInfoMapper.selectByPrimaryKey(id);
24      }
25 }
UserInfoService

2. Mapper層申明

1 package com.disappearwind.mapper;
2 
3 import com.disappearwind.model.UserInfo;
4 
5 public interface UserInfoMapper {
6     UserInfo selectByPrimaryKey(Integer id);
7 }
UserInfoMapper

3. mpper.xml配置檔案

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 
 4 <mapper namespace="com.disappearwind.mapper.UserInfoMapper" >
 5 
 6   <resultMap id="BaseResultMap" type="com.disappearwind.model.UserInfo">
 7         <id column="UserInfoID" property="userinfoid" jdbcType="INTEGER" />
 8         <result column="Name" property="username" jdbcType="VARCHAR" />
 9         <result column="Phone" property="phone" jdbcType="CHAR" />
10         <result column="Pwd" property="pwd" jdbcType="CHAR" />
11     </resultMap>
12 
13      <sql id="Base_Column_List">
14         UserInfoID, Name, Type, TypeRemark, HeadUrl,BigImgUrl,GreatImgUrl,
15         NickName, Sex, Status,Labels, StoryCount,
16         FriendCount, FollowerCount, FavouriteCount, HotNum,
17         CreateDate,Description
18     </sql>
19   
20   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
21     select 
22     <include refid="Base_Column_List" />
23     from userinfo
24     where UserInfoID = #{userinfoid,jdbcType=INTEGER}
25   </select>
26     
27 </mapper>
UserInfoMapper.xml

4. 實體model層

 1 package com.disappearwind.model;
 2 
 3 public class UserInfo {
 4     private Integer userinfoid;
 5 
 6     private String username;
 7 
 8     private String phone;
 9 
10     private String pwd;
11     
12     public Integer getUserinfoid() {
13         return userinfoid;
14     }
15 
16     public void setUserinfoid(Integer userinfoid) {
17         this.userinfoid = userinfoid;
18     }
19 
20     public String getUsername() {
21         return username;
22     }
23 
24     public void setUsername(String username) {
25         this.username = username;
26     }
27 
28     public String getPhone() {
29         return phone;
30     }
31 
32     public void setPhone(String phone) {
33         this.phone = phone;
34     }
35 
36     public String getPwd() {
37         return pwd;
38     }
39 
40     public void setPwd(String pwd) {
41         this.pwd = pwd;
42     }
43 
44     public UserInfo() {
45     }
46 }
UserInfo

5. SpringMVC配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-4.0.xsd
11         http://www.springframework.org/schema/aop
12         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
13         http://www.springframework.org/schema/tx
14         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
15         ">
16     <context:component-scan base-package="com.disappearwind.*" />
17 </beans>
context.xml

注意:context.xml的位置在web.xml中的如下配置節配置

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:context.xml</param-value>
</context-param>

 

用此方案的好處:省得寫DAO層,只要Mapper層的方法申明和mapper.xml的方法申明保持一致,並且檔名也保持一致(UserInfoMapper.java和UserInfoMapper.xml)就能順利的訪問

相關文章