Mybatis基于注解的方式访问数据库
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基于注解的方式访问数据库的更多相关文章
- Mybatis框架基于注解的方式,实对数据现增删改查
编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...
- 对比传统方式访问数据库和SpringData访问数据库
我们在写代码的时候应该一边写一边测试,这样的话可以尽快的找到错误,在代码写多了之后去找错误的话不容易给错误定位 传统方式访问数据库 1:创建一个Maven web项目 2:修改pom.xml为以下内容 ...
- MyBatis学习(三)MyBatis基于动态代理方式的增删改查
1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...
- 非链接方式访问数据库--查询的数据集用Dataset来存储。
private void Button_Click_1(object sender, RoutedEventArgs e) { //非链接方式访问数据库, //1创建连接对象(连接字符串) using ...
- ADO.NET 连接方式和非链接方式访问数据库
一.//连接方式访问数据库的主要步骤(利用DataReader对象实现数据库连接模式) 1.创建连接对象(连接字符串) SqlConnection con = new SqlConnection(Co ...
- php 面向对象的方式访问数据库
<body> <?php //面向对象的方式访问数据库 //造对象 $db = new MySQLi("localhost","root",& ...
- 使用Spring框架入门四:基于注解的方式的AOP的使用
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
- 基于注解的方式管理Bean
--------------------siwuxie095 基于注解的方式管理 Bean (一)准备 ...
- SpringMvc+Spring+MyBatis 基于注解整合
最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方 ...
随机推荐
- hdu4831 Scenic Popularity(线段树)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4831 题目大概意思就是有多个风景区和休息区,每个风景区有热度,休息区的热度与最接近的分景区的热度相同, ...
- Linux分区:超过2TB硬盘分区
测试iscsi服务是否正常 [root@FocusBackup ~]# service iscsi restart 停止 iscsi: ...
- CSS3动画几个平时没注意的属性
一.timing-function: steps() 一开始在使用CSS3的时候并没有太注意这个timing-function,只是注意到自定义贝塞尔曲线. 1)一个项目中的实例 先来看看左边加了st ...
- 使用Oracle调度程序自动完成任务
1. 创建作业.计划和时间表 2. 创建轻量级作业 3. 使用作业链执行一系列相关任务 4. 创建窗口和作业类 5. 使用高级调度程序概念确定作业优先顺序 Reference 实验演示准备: --业务 ...
- JavaScript基础深入研究
一.DOM事件 1.事件阻止API preventDefault() — 阻止浏览器默认 stopPropagation() — 阻止事件流冒泡 stopImmediatePropagation() ...
- AGS中通过FeatureServer插入数据失败、插入数据在WMTS请求中无法显示以及version概念的讨论
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 在多个项目中,当我方接口给其他部门人员使用时出现了插入数据失 ...
- T-SQL CROSS APPLY、MERGE
写在前面 刚才看项目里一个存储过程,也是好长时间没有使用Sql Server2008了,好多写法和函数感觉到陌生,这就遇到了CROSS APPLY 和MERGE的语法,两者之前完全没接触过. 所以专门 ...
- Android 短信监听及用途分析
监听系统短信这个只能作为一个技术点来研究下,读者可能在工作中可能不会哦涉及到,一般的应用软件也不会有这个需求 但是作为程序员呢,多了解一下也是好的. Android 监听系统短信有什么用? 1.对系统 ...
- OpenCV2:Mat属性type,depth,step
在OpenCV2中Mat类无疑使占据着核心地位的,前段时间初学OpenCV2时对Mat类有了个初步的了解,见OpenCV2:Mat初学.这几天试着用OpenCV2实现了图像缩小的两种算法:基于等间隔采 ...
- 使用Apache Server 的ab进行web请求压力测试
参考:http://www.cnblogs.com/spring3mvc/archive/2010/11/23/2414741.html 自己写代码经常是顺着逻辑写下去,写完后run一下,ok就玩完事 ...