1. 使用方式:在Service层直接调用
package com.disappearwind.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; import com.disappearwind.mapper.UserInfoMapper;
import com.disappearwind.model.UserInfo; /**
* 用户service
*
*/
@Service
@Repository
public class UserInfoService{ @Autowired
private UserInfoMapper userInfoMapper; public UserInfo selectByPrimaryKey(Integer id){
return userInfoMapper.selectByPrimaryKey(id);
}
}
UserInfoService
2. Mapper层申明
package com.disappearwind.mapper; import com.disappearwind.model.UserInfo; public interface UserInfoMapper {
UserInfo selectByPrimaryKey(Integer id);
}
UserInfoMapper
3. mpper.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="com.disappearwind.mapper.UserInfoMapper" > <resultMap id="BaseResultMap" type="com.disappearwind.model.UserInfo">
<id column="UserInfoID" property="userinfoid" jdbcType="INTEGER" />
<result column="Name" property="username" jdbcType="VARCHAR" />
<result column="Phone" property="phone" jdbcType="CHAR" />
<result column="Pwd" property="pwd" jdbcType="CHAR" />
</resultMap> <sql id="Base_Column_List">
UserInfoID, Name, Type, TypeRemark, HeadUrl,BigImgUrl,GreatImgUrl,
NickName, Sex, Status,Labels, StoryCount,
FriendCount, FollowerCount, FavouriteCount, HotNum,
CreateDate,Description
</sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from userinfo
where UserInfoID = #{userinfoid,jdbcType=INTEGER}
</select> </mapper>
UserInfoMapper.xml
4. 实体model层
package com.disappearwind.model; public class UserInfo {
private Integer userinfoid; private String username; private String phone; private String pwd; public Integer getUserinfoid() {
return userinfoid;
} public void setUserinfoid(Integer userinfoid) {
this.userinfoid = userinfoid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public UserInfo() {
}
}
UserInfo
5. SpringMVC配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<context:component-scan base-package="com.disappearwind.*" />
</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)就能顺利的访问