惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 消失的风

富文本编辑器vue2-editor实现全屏功能 使用Mist部署Contract到Rinkeby以太坊网络 基于以太坊的Token开发步骤 技术顾问认知(一) Angularjs-项目搭建 Augularjs-起步 appfuse:Excel导出 Appfuse:添加自定义页面组件 基于JQuery实现的文本框自动填充功能 Appfuse:权限控制 Appfuse:记录操作日志 Appfuse:扩展自己的GenericManager Appfuse:第一张表维护 Appfuse:起步 JAVA实现图片裁剪 敏捷开发与jira之研发管理模式 敏捷开发与jira之阶段工作项概述 敏捷开发与jira之燃烧图 敏捷开发与jira之流程
Mybatis基于注解的方式访问数据库
消失的风 · 2015-07-03 · via 博客园 - 消失的风

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)就能顺利的访问