博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot +Spring+ Mybatis的搭建
阅读量:7039 次
发布时间:2019-06-28

本文共 5442 字,大约阅读时间需要 18 分钟。

hot3.png

最近新项目要抛弃以前的SSH框架结构,采用最新的RESTful框架结构去实现。

做到前后端分离。数据库方面准备采用Mybatis框架去操作。

RESTful web Service的搭建参考官方文档:

RESTful设计风格通俗来讲 就是,合理使用HTTP协议(POST,GET,PUT,DELETE)来请求服务。服务将对应的信息以JSON格式返回前台,而不再需要提交请求给服务器,服务器处理请求,转发给页面。这个设计风格直接将服务器上的每个模块抽象成一个个资源。而前端页面做的 就是访问资源,获取资源信息,展示即可。

接下来展示SpringBoot+Spring+mybatis的配置

其中采用Maven管理Jar包 其中的pom.xml如下:

1.6
5.1.36
3.3.1
1.2.4
3.3.6
4.1.1
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-test
mysql
mysql-connector-java
com.fasterxml.jackson.core
jackson-core
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.datatype
jackson-datatype-joda
com.fasterxml.jackson.module
jackson-module-parameter-names
com.alibaba
druid
1.0.11
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
org.mybatis.generator
mybatis-generator-core
1.3.2
compile
true
com.github.pagehelper
pagehelper
${pagehelper.version}
tk.mybatis
mapper
${mapper.version}

在JAR包下载好之后,配置applicationContext.xml文件。这个文件是Spring的核心文件。我们将在这个文件里配置MyBatis的数据库配置,applicationContext.xml配置如下:

注:这里的sessionFactory 关联的类是 org.mybatis.spring.SqlSessionFactoryBean 这个是mybaits的会话工厂。其中的两个参数dataSource:配置数据源,mapperLocations:配置Mybatis的Mapping文件。

这个只是配置了applicationContext.xml 如何告诉SpringBoot启动的时候加载这个文件呢。如下:

import org.apache.catalina.LifecycleException;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {	    public static void main(String[] args) throws LifecycleException {    	//通过注解加载Bean//    	SpringApplication.run(Application.class, args);    	//从Spring配置文件加载Bean    	SpringApplication.run("classpath:applicationContext.xml", args);//    	SpringApplication app=new SpringApplication(Application.class);//    	app.run(args);    }}

这样 在SpringBoot启动的时候 就会去加载applicationContext.xml文件。这样Spring,SpringBoot,Mybatis就关联起来了。SpringBoot是通过注解去发现Bean 详情参考文档开头Spring的官方文档

Mybatis的使用,在Dao层 有三种方案。

  1. 继承org.mybatis.spring.SqlSessionTemplate
  2. 继承org.mybatis.spring.support.SqlSessionDaoSupport
  3. 继承org.mybatis.spring.SqlSessionFactoryBean

分别调用对应的方法去执行SQL 同Mybatis使用。上述三种继承方案有不同的处理方法,

第一种方法,继承SqlSessionTemplate后 由于这个类重载了三个带参构造器,继承之后  需要实现他的一个构造器。将SessionFactory赋值进去。然后通过sessionFactory来获取sqlSession

160431_4SKd_2970507.png

160450_YCoJ_2970507.png

第二种方法,继承SqlSessionDaoSupport 后 可以发现 只提供了sqlSession变量可以使用。但是sqlSession来自sqlSessionFactory 所以 可以必须在这里调用setSqlSessionFactory方法。否则你拿到的sqlsession是一个NULL

160032_9WkF_2970507.png

第三种方法:继承SqlSessionFactoryBean 之后 查看buildSqlSessionFactory 方法 如下:

160841_qf1Z_2970507.png

如源代码所示,你得先配置他的configuration.这个是你配置Mybatis的配置文件。这个类功能相当于读取配置文件,重新创建sqlSessionfactory。如下:

public class MybatisTest {	public static void main(String[] args) throws IOException {		Reader reader=Resources.getResourceAsReader("Mybatis.xml");		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);		SqlSession sqlsession=sessionFactory.openSession();//		user list=sqlsession.selectOne("SSI.user.selectUser", "902889");		List
list=sqlsession.selectList("selectAll"); System.out.println(list); }}

以上,右键application。整个项目启动。

效果如下

093000_5ouv_2970507.png

至此整个项目就搭建好了。

 

转载于:https://my.oschina.net/u/2970507/blog/789188

你可能感兴趣的文章
Android之permission权限列表(AndroidManifest.xml)
查看>>
Java算法练习——盛最多水的容器
查看>>
centos7/RHEL7最小化系统安装gnome图形界面
查看>>
zabbix添加ceph监控
查看>>
视频FMS服务器带宽成本分析
查看>>
django--认证系统
查看>>
Qt学习二、添加资源文件
查看>>
webkit学习之event
查看>>
Redux入门学习
查看>>
我的友情链接
查看>>
zabbix监控系统之使用ztc mysql模板
查看>>
磊哥最近所想
查看>>
MyBatis mapper文件中的变量引用方式#{}与${}的差别
查看>>
数据库技能实战进阶之常用结构化sql语句(上)
查看>>
Apache2.2+mod_encoding解决URL中文编码问题-2008.1.8
查看>>
Dockerfile创建镜像
查看>>
struts2学习——ValueStack
查看>>
IV 12 MySQL+drbd+heartbeat
查看>>
Python实现删除目录下相同文件
查看>>
求100内质数的个数
查看>>