Mybatis accesses the database through annotation, and mybatis accesses data
1. Usage: directly called at the Service layer
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 * User 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 layer Declaration
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 configuration file
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" 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. entity model layer
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 Configuration
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
Note: the location of context. xml is configured in the following configuration section in web. xml.
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value> classpath: context. xml </param-value>
</Context-param>
Advantages of using this scheme: it saves the write DAO layer, as long as the ER layer's method declaration and Mapper. the method declaration in xml is consistent, and the file name is also consistent (UserInfoMapper. java and UserInfoMapper. xml ).