关于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. 设计模式——设计模式之禅day2

    接口隔离原则 接口分为两种: ● 实例接口( Object Interface) , 在Java中声明一个类, 然后用new关键字产生一个实例, 它是对一个类型的事物的描述, 这是一种接口. 比如你定 ...

  2. T-SQL 使用链接库向mysql导数据遇到的奇葩事件一

    mysql表结构有 主键 非自增 text longtext类型字段多个 步骤 1.在T-SQL 临时表中处理好所有需要的字段 2.执行openquery语句 字段顺序完全按照mysql字段顺序插入 ...

  3. 数组和ArrayList 相互转换

    Dim params(10) As DbParameter params(0) = DBHelper.CreateDbParameter("@RegName", Jz_RegInf ...

  4. Swift泛型和泛型函数

    泛型(generic)可以使我们在程序代码中定义一些可变的部分,在运行的时候指定.使用泛型可以最大限度地重用代码.保护类型的安全以及提高性能.在Swift集合类中,已经采用了泛型.一.一个问题的思考怎 ...

  5. iOS开发——时间格式类

    目前只实现了三个类方法, 第一个获取当前时间,以字符创的形式返回,例如"201606161532" 第二个以当前时间与给定时间的时间差(秒) 第三个以当前时间与给定时间的时间差(分 ...

  6. oracle表分区心得

    由于系统是对前批次系统进行改造,需要对表建立分区 1.已建立未分区的表,无法进行任何表分区的操作,如:增加.删除.合并.拆分均无法操作 2.已分区的表至少保留1个分区,即不能全删 3.若有defaul ...

  7. 《RedHatLinux系统修复视频(通过本地镜像)》

    此视频的测试环境: win7下安装的VMware    redhatlinux6.3系统的修复     以删掉kernel系统引导故障重新安装kernel为例 基于本地镜像来对系统进行修复 如疑问可留 ...

  8. mysql table readonly

    if the table does not have primary key or unique non-nullable defined, then MySql workbench could no ...

  9. 文件大小的友好输出及其 Python 实现

    在数据库中存储时,使用 Bytes 更精确,可扩展性和灵活性都很高. 输出时,需要做一些适配. 1. 注意事项与测试代码 需要考虑 sizeInBytes 为 None 的场景. 除以 1024.0 ...

  10. Thinkcmf 在新浪云上的部署问题

    最近要开发一个社团主页,于是想到了CMF内容管理系统的,但是直接在自己的服务器测试成本太高,于是选择了在新浪云上进行部署测试. 但是在安装Thinkcmf的过程中产生了一些技术性的问题.但最后终于在自 ...