首页 > 学院 > 开发设计 > 正文

Mybatis基于注解的方式访问数据库

2019-11-15 00:45:05
字体:
来源:转载
供稿:网友
Mybatis基于注解的方式访问数据库

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  * 用户service13  *14  */15 @Service16 @Repository17 public class UserInfoService{18     19     @Autowired20     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,Description18     </sql>19   20   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >21     select 22     <include refid="Base_Column_List" />23     from userinfo24     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/context10         http://www.springframework.org/schema/context/spring-context-4.0.xsd11         http://www.springframework.org/schema/aop12         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd13         http://www.springframework.org/schema/tx14         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd15         ">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)就能顺利的访问


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表