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

Mybatis基于注解的方式访问数据库的更多相关文章

  1. Mybatis框架基于注解的方式,实对数据现增删改查

    编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...

  2. 对比传统方式访问数据库和SpringData访问数据库

    我们在写代码的时候应该一边写一边测试,这样的话可以尽快的找到错误,在代码写多了之后去找错误的话不容易给错误定位 传统方式访问数据库 1:创建一个Maven web项目 2:修改pom.xml为以下内容 ...

  3. MyBatis学习(三)MyBatis基于动态代理方式的增删改查

    1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...

  4. 非链接方式访问数据库--查询的数据集用Dataset来存储。

    private void Button_Click_1(object sender, RoutedEventArgs e) { //非链接方式访问数据库, //1创建连接对象(连接字符串) using ...

  5. ADO.NET 连接方式和非链接方式访问数据库

    一.//连接方式访问数据库的主要步骤(利用DataReader对象实现数据库连接模式) 1.创建连接对象(连接字符串) SqlConnection con = new SqlConnection(Co ...

  6. php 面向对象的方式访问数据库

    <body> <?php //面向对象的方式访问数据库 //造对象 $db = new MySQLi("localhost","root",& ...

  7. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  8. 基于注解的方式管理Bean

    --------------------siwuxie095                                 基于注解的方式管理 Bean         (一)准备         ...

  9. SpringMvc+Spring+MyBatis 基于注解整合

    最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方 ...

随机推荐

  1. (转)对Lucene PhraseQuery的slop的理解

    所谓PhraseQuery,就是通过短语来检索,比如我想查"big car"这个短语,那么如果待匹配的document的指定项里包含了"big car"这个短语 ...

  2. 基于表的数据字典构造MySQL建表语句

    表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...

  3. ASP.NET MVC Ajax.ActionLink 简单用法

    ASP.NET MVC 项目中,如何使用类似于 iframe 的效果呢?或者说 Ajax 局部刷新,比如下面操作: 我们想要的效果是,点击 About 链接,页面不刷新(地址栏不变),然后下面的内容进 ...

  4. hibernate笔记--组合主键映射方法

    一个数据库表中其主键有可能不止一个属性,同样映射到实体类中,可能有两个或多个属性共同配置成为一个主键,假设一个实体类Score,其主键有两个属性stuId(学生编号)和subjectId(科目编号), ...

  5. spring boot启用tomcat ssl

    首先要生成一个keystore证书.参考:Tomcat创建HTTPS访问,java访问https,ssl证书生成:cer&jks文件生成摘录,spring-boot 这里复现一下完整过程: 安 ...

  6. JS正则表达式总结

    关于JS的正则用法,已经有很多文章了,大同小异 正则表达式30分钟入门教程 MDN正则表达式 玩转JavaScript正则表达式 ES6正则的扩展

  7. Linux环境中Openfire安装指南

    Linux环境中Openfire安装指南 安装环境: 安装软件:Openfire 4_1_0 http://download.igniterealtime.org/openfire/openfire_ ...

  8. rsa互通密钥对生成及互通加解密(c#,java,php)

    摘要 在数据安全上rsa起着非常大的作用,特别是数据网络通讯的安全上.当异构系统在数据网络通讯上对安全性有所要求时,rsa将作为其中的一种选择,此时rsa的互通性就显得尤为重要了. 本文参考网络资料, ...

  9. Web 上传图片加水印

    上传图片加水印 需要使用控件FileUpload 上传按钮Image控件展示上传的图片,页面中拖入三个控件 <form id="form1" runat="serv ...

  10. 【原创】SQL常用函数

    --值类型转换 ) --时间差 ,GETDATE()),GETDATE()) --加减指定时间 ,GETDATE()) --获取系统日期 select GETDATE() --生成不重复Id sele ...