关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出。在此,记录一下几种通过MyBatis访问数据库的方式。

1、在spring框架中使用mybatis来进行数据操作配置,参考Spring+MyBatis实践—工程配置的spring-datasources.xml文件。

2、Demo1—通过sqlSessionTemplate来进行数据库访问:

首先,定义一实体类User,并且在数据库(MySQL)中建立了相应的数据表。

 public class User {
private int userId;
private String name;
private String pwd;
private String email;
private String address;
private String signature;
private String phone; /*构造器和getter、setter方法*/
}

接着,编写对数据库中表tb_user的映射文件User.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.crazysnail.dao.UserDao">
<select id="getUserByUserId" resultType="User" parameterType="int">
select *
from tb_user
where userid=#{userId}
</select>
</mapper>

【注:注意映射文件开始处指定的命名空间。】

此时,可以通过sqlSessionTemplate来进行数据访问了。

@Service
public class UserService {
@Autowired
private SqlSessionTemplate sqlSessionTemplate; public User getUserByUserId(int userId){
User user = (User)sqlSessionTemplate.selectOne("com.crazysnail.dao.UserDao.getUserByUserId", 1); return user;
}
}

【注:sqlSessionTemplate的selectOne方法中的com.crazysnail.dao.UserDao.getUserByUserId要对应到映射文件中的select语句的id,参数通过selectOne的第二个进行传入。】

3、Demo2—通过Dao接口来进行数据库访问。

通过上述方式来访问数据库,比较繁琐,不直观。此时,可以采用另外一种方式来调用映射文件User.xml中的数据库操作。

接着Demo1所做的工作,继续编写UserDao接口。

 package com.crazysnail.dao;

 import com.crazysnail.domain.User;

 public interface UserDao {
public User getUserByUserId(int userId); }

在spring的配置文件中添加如下配置(该配置已经在Spring+MyBatis实践—工程配置的spring-datasources.xml文件中添加):

<!-- 用于将接口映射为具体的实例 ,使得在Service中可以直接注入相关的Dao接口来进行数据访问-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:sqlSessionFactory-ref="sqlSessionFactory"
p:basePackage="com.crazysnail.dao"
/>

该bean用于将Dao接口映射到对应的映射文件,其中映射文件的命名空间对应Dao接口的全称。这就使得UserDao接口同User.xml文件关联起来。

【说明:关联关系的建立,是通过在User.xml文件定义时指定的命名空间名称。User.xml的命名空间定义为com.crazysnail.dao.UserDao,正是UserDao接口的全称。同时,需要注意,在UserDao接口中声明的方法名要对应到User.xml中定义的数据访问过程的id,接口中方法的形参类型对应到User.xml中数据访问过程的parameterType,方法的形参名对应到User.xml中数据访问过程中sql语句中的参数,接口方法的返回值类型对应User.xml中的resultType声明的类型。

如UserDao接口中的方法,

 public User getUserByUserId(int userId);

对应User.xml中的

<select id="getUserByUserId" resultType="User" parameterType="int">
select *
from tb_user
where userid=#{userId}
</select>

说明结束。】

最后,就可以在service中通过注入UserDao,调用UserDao中声明的接口来进行数据处理了。

 @Service
public class UserService {
@Autowired
private UserDao userDao; public User getUser(int id){
return userDao.getUserByUserId(id);
}
}

注意:使得MyBatis的数据访问的Xml生效,需要在配置SqlSessionFactoryBean时,通过p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml"进行声明。

4、Demo3—省略映射文件,使用注解:

MyBatis的org.apache.ibatis.annotations包中提供的关于数据访问的注解包括@Select、@Update、@Delete、@Insert,可以用来直接注入简单的SQL语句,省略映射文件。也可映射文件、注解两者结合使用。

单独使用注解时,可以省略掉

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatisConfig.xml"
p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml"
/>

中的p:mapperLocations="classpath:com/crazysnail/domain/mybatisConfig/*.xml" 关于映射文件位置的配置。

实例,改写Demo2中的Dao接口,并将映射文件的相关配置去掉,Service层中的代码不改变即可。

  package com.crazysnail.dao;

  import com.crazysnail.domain.User;

  public interface UserDao {
@Select("select * from tb_user where userid=#{userId}")
public User getUserByUserId(int userId); }

Spring+MyBatis实践—MyBatis数据库访问的更多相关文章

  1. Spring Mvc和Mybatis的多数据库访问配置过程

    Spring Mvc 加Mybatis的多数据库访问源配置访问过程如下: 在applicationContext.xml进行配置 <?xml version="1.0" en ...

  2. Spring Boot初探之数据库访问

    一.背景 Spring boot是集服务发布.数据库管理.日志管理等于一身的服务开发框架:是微服务开发的全能小帮手.这章讲述一下如何使用spring boot访问MySQL数据库. 二.搭建基础环境 ...

  3. Spring Boot实践——Mybatis分页插件PageHelper的使用

    出自:https://blog.csdn.net/csdn_huzeliang/article/details/79350425 在springboot中使用PageHelper插件有两种较为相似的方 ...

  4. Spring+Mybatis+Mysql搭建分布式数据库访问框架

    一.前言 用Java开发企业应用软件, 经常会采用Spring+MyBatis+Mysql搭建数据库框架.如果数据量很大,一个MYSQL库存储数据访问效率很低,往往会采用分库存储管理的方式.本文讲述如 ...

  5. [spring+springmvc+mybatis实践]学生社团管理系统

    一.简介 ssm框架为现在十分流行的mvc主流框架.mybatis负责与数据库交互,springmvc与spring完美适配,负责控制器和视图渲染.之前有初步学习过ssm框架,这次借学校里的web课设 ...

  6. Spring Boot 实践 :Spring Boot + MyBatis

    Spring Boot 实践系列,Spring Boot + MyBatis . 目的 将 MyBatis 与 Spring Boot 应用程序一起使用来访问数据库. 本次使用的Library spr ...

  7. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  8. Spring Boot 集成 MyBatis和 SQL Server实践

    概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...

  9. (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】

    大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...

随机推荐

  1. 解决TextView在显示文字的时候,一行还没显示满就跳到下一行

    转载请注明:转自 http://blog.csdn.NET/u011176685/article/details/48295185 一.问题描述: Android的TextView在显示文字的时候,如 ...

  2. plsqldev与sqldeveloper

    plsqldev连接 1.连接不同服务器,要修改tnsnames.ora文件,具体如下修改如下位置 # tnsnames.ora Network Configuration File: \app\us ...

  3. Xamarin 实现android gridview 多选

    参考文章:http://blog.csdn.net/zhouyuanjing/article/details/8372686 GridView初始化代码: gridViewStudent = Find ...

  4. style、currentStyle、getComputedStyle区别介绍

    style.currentStyle.getComputedStyle区别介绍 来自:蓝色天空 样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有 ...

  5. [DevExpress]ChartControl之饼状图百分比示例

    关键代码: using System; using System.Data; using System.Windows.Forms; using DevExpress.XtraCharts; name ...

  6. GridView - javascript 触发后台 OnSelectedIndexChanged

    1.ASPX <asp:GridView ID="gdvDealers" runat="server" AutoGenerateColumns=" ...

  7. jquery nicescroll 配置参数

    jQuery滚动条插件兼容ie6+.手机.ipad http://www.areaaperta.com/nicescroll/ jQuery(function($){ $("#scrollI ...

  8. [Linux]学习笔记(1)

    说到Linux就不得不提UNIX,因为Linux是从UNIX系统发展来的,两系统极为相似,可以在UNIX操作系统上使用的功能都可以在Linux上使用,只可能有少许的差异: UNIX系统中所有的东西都是 ...

  9. mysql实例---sql语句中使用@变量

    本文介绍下,在mysql语句中使用@变量的一个例子,学习下这个特殊变量的用法,有需要的朋友参考下吧. 要求: 计算用户距上次访问的天数,根据imei号区分不同的用户,如果时间段内只有一次访问则为0. ...

  10. ubuntu 安装dell无线网卡2

    以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...