在http://dufengx201406163237.iteye.com/blog/2102054中描述了工程的配置,在此记录一下如何使用MyBatis访问数据库;
1、主要配置为:
<!-- 其中p:mapperLocations指定数据库操作文件的地址 --> <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" /> <!-- mybatis数据访问的核心模板 --> <bean class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory" /> </bean>
在Spring的配置文件中配置了sqlSessionTemplate,你可以通过在Service文件中注入sqlSessionTemplate来访问数据库,就像通过Spring为JDBC提供的jdbcTemplate来访问数据库一样。
例如,定义了一User实体类,并且在数据库中设计了相应的数据表;
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方法*/ }
接下来编写对数据库中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> <select id="countUser" resultType="int" parameterType="User"> select count(*) from tb_user where email=#{email} and pwd=#{pwd} </select> <update id="updateUserInfo" parameterType="User"> update tb_user t set t.pwd=#{pwd}, t.name=#{name} where t.userid={#userId} </update> <insert id="addUser" parameterType="User" useGeneratedKeys="true" keyProperty="userid"> insert into tb_user(email,name, pwd) values(#{email}, #{name},#{pwd}) </insert> <select id="getUserId" parameterType="String" resultType="int"> select t.userid from tb_user t where t.email = #{email} </select> <select id="getUserByEmail" parameterType="String" resultType="User"> select * from tb_user where email = #{email} </select> </mapper>
其中需要注意的是,User.xml文件开始处指定的命名空间;
此时,你可以通过在service文件中通过注入sqlSessionTemplate,调用User.xml文件中通过select、update、delete、insert定义的数据访问的过程;
@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 Bean提供的接口来进行数据访问时,接口参数需要对应到User.xml定义时指定的命名空间和数据访问的id。
2、通过接口来调用映射文件中声明的数据访问过程;
通过上述方式来访问数据库,比较繁琐,不直观。此时,可以采用另外一种方式来调用像是User.xml这种数据库操作的xml文件。
首先声明接口UserDao.java;
package com.crazysnail.dao; import com.crazysnail.domain.User; public interface UserDao { public int countUser(User user); public User getUserByUserId(int userId); public void updateUserInfo(User user); public void addUser(User user); public int getUserId(String email); public User getUserByEmail(String email); }
接着,在Spring的配置文件中添加如下配置;
<!-- 用于将接口映射为具体的实例 ,使得在Service中可以直接注入相关的Dao接口来进行数据访问--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:sqlSessionFactory-ref="sqlSessionFactory" p:basePackage="com.crazysnail.dao" />
通过配置MapperScannerConfigurer,可以将定义的接口文件与对应的数据库操作文件关联起来,如将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"进行声明。
相关推荐
基于springmvc+spring+mybatis有数据库的文件的税控系统,导入myEclipse可直接运行
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) http://localhost:8080/HNUST/user/showUser?id=1 数据库改为自己的数据库
SpringBoot+mybatis-plus+VUE民宿管理系统后台-毕业设计 ...———————————————— 版权声明:本文为CSDN博主「项目花园范德彪」的原创文章, https://blog.csdn.net/dearmite/article/details/132137913
后台框架: SSM(SpringMVC + Spring + Mybatis) 用户功能: 实现注册、登录; 实现在留言板里留言; 公告区; 习题模块:不做练习,每种语法————只有5道例题,每道有解析即可 视频播放模块:对视频进行分类,可以...
基于SpringBoot+Mybatis+Redis开发的一个问答社区源码+sql数据库(仿牛客网).zip 【项目介绍】 主要功能: 使用 ThreadLocal 保存用户状态,通过拦截器拦截请求,根据自定义注解判断用户登录状态 使用 Ajax 异步发帖...
完整的Demo结合了springmvc——mybatis,实现了工具类文件上传下载,结合了Redis的初步使用,并且能使用threadlocal实现数据库动态切换,很适合初建项目做参考,适合初学者使用。
基于SSM(Sping+Spring MVC+MyBatis)框架的校园宽带业务管理系统.zip # GZHMTJavaEE课设项目——校园宽带业务管理系统 本项目内容JavaEE期末课设,将保持持续更新; ## 目录 * [GZHMTJavaEE课设项目——校园宽带...
———————————————— 版权声明:本文为CSDN博主「项目花园范德彪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:...
开发环境STS,项目完整,只需修改数据库连接配置文件即可,数据库表创建文件已给出。
SSM,即:Spring + Spring MVC + MyBatis ———————————————————— (1)基于Java的SSM在线图书销售管理系统; (2)前台页支持图书直接下单,也支持加入购物车后批量下单; (3)后台支持商品管理...
基于SSM实现的客户关系管理系统+数据库+项目说明(毕设项目).zip 【项目环境】 操作系统: Windows_7 集成开发工具: Eclipse EE_4.7 编译环境:JDK_1.8 Web服务器:Tomcat_9.0 数据库:MySQL_5.7.23 【系统框架】 ...
该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 装饰工程管理系统——项目立项子系统可以提高装饰工程项目信息管理问题的解决效率,优化装饰工程项目信息处理流程,保证装饰工程项目信息...
该毕业设计模仿了微博的功能,使用技术:spring mvc,spring,mybatis,websocket,页面较为清爽。 系统功能包括:分享新鲜事,点赞,收藏,回复等。因为使用了websocket,所以当别人点赞或者回复的时候,服务器端会...
MyBatis框架的学习(六)——MyBatis整合Spring
为了方便管理教师的信息,统一进行管理,可以把所有教师的信息录入系统,及时的进行修改,删除,查询...后端使用spring+springMVC+mybatis框架搭建,前端界面使用jsp编写,数据库使用mysql,用Navicat进行数据库管理。
项目涉及技术栈: 1. layui框架 2. ssm框架(spring + spring mvc + mybatis) 3. mysql数据库
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 最近在学习Spring+SpringMVC+MyBatis的整合。以下是参考网上的资料自己实践操作的详细步骤。 1、基本概念 1.1、Spring Spring是一个开源框架...
2、使用数据库连接池技术,使用mybatis框架完成底层实体类的创建和数据访问层的实现。 3、实现登录功能。 4、实现登录成功后全部商品分页显示、翻页功能。 5、实现商品增加功能。 6、实现商品删除功能、批量删除...
基于Spring + Spring MVC + MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 项目演示 演示地址 <——点击跳转 ...
框架:Spring + Spring MVC + MyBatis 服务器:Tomcat 前端解析框架:Thymeleaf 开发工具:Idea 2017 版本管理工具:Maven 版本控制工具:GitHub 搭建数据库 MySQL driver=com.mysql.jdbc.Driver url=...