springboot+springmvc+mybatis项目整合
介绍:
上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生,pivotal团队提供了一款全新的框架,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,spring boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
特点:
1. 创建独立的spring应用程序
2. 嵌入的tomcat,无需部署war文件
3. 简化maven配置
4. 自动配置spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对xml没有要求配置
(以上大部分内容摘自百度百科)
ok,关于springboot的讲解就到这里大家想了解的更详细可以自行百度。
搭建springboot项目我推荐大家用idea或者sts(spring tool suite spring公司自己研发的一款编辑器),我现在用的是idea,所以接下来我是用idea搭建项目的
一、创建项目
填写完group和atrifact后点击下一步,这里我选择的是jar,因为官方文档推荐的是打包成jar,所以这里就不过多解释了。
这里勾选上web
这里勾选上mysql、jdbc和mybatis点击下一步
这里输入项目名和项目路径后点击完成
这是新建完成后的项目结构
二、添加pom.xml依赖
因为springboot不推荐使用jsp做页面,如果想要使用就必需的添加jsp的依赖
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.chaoqi</groupid> <artifactid>springboot_demo2</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot_demo2</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.0.release</version> <relativepath/> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.3.2</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!--添加jsp依赖 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
三、springboot整合springmvc
因为在创建项目的时候我们选择了mybatis和jdbc所以在这里也要把他们两也给配置进去
编辑application.properties
# 页面默认前缀目录 spring.mvc.view.prefix=/web-inf/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp #开发配置 spring.datasource.driverclassname = com.mysql.jdbc.driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8 spring.datasource.username = root spring.datasource.password = 123456 # mybatis接口文件位置 mybatis.mapper-locations: classpath:mapper/*.xml mybatis.type-aliases-package: com.chaoqi.springboot_demo2.domain
如果大家习惯用application.yml那也可以用,但是用application.yml在第一次启动项目的时候一定要maven clean一下,不然会报错。
server: port: 8080 spring: mvc: view: prefix: /web-inf/jsp/ suffix: .jsp datasource: url: jdbc:mysql://localhost:3306/test?characterencoding=utf-8&useunicode=true&usessl=false username: root password: 123456 driver-class-name: com.mysql.jdbc.driver mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.chaoqi.springboot_demo2.domain
编辑完application.properties之后再src/mian下创建webapp目录,结构如下
新建indexcontroller
package com.chaoqi.springboot_test.web; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; @controller public class indexcontroller { private static final string index = "index"; @requestmapping("/show") public string getindex() { return index; } }
运行main函数
访问页面,成功
四、springboot整合mybatis
创建数据库表
-- ---------------------------- -- table structure for music_info -- ---------------------------- drop table if exists `music_info`; create table `music_info` ( `id` int(10) not null auto_increment comment '主键id', `singer_name` varchar(100) not null comment '歌手名', `music_size` varchar(100) not null comment '歌曲大小', `music_name` varchar(100) not null comment '歌曲名', primary key (`id`) ) engine=innodb auto_increment=4 default charset=utf8; -- ---------------------------- -- records of music_info -- ---------------------------- insert into `music_info` values ('1', '小三', '3.2m', '起风了'); insert into `music_info` values ('2', '刘德华', '3.0m', '忘情水'); insert into `music_info` values ('3', '猪点点', '5.0m', '会写程序的小猪');
创建pojo
package com.chaoqi.springboot_test.dao.domain; public class musicinfo { // 主键id private integer id; // 歌手名 private string singername; // 歌曲大小 private string musicsize; // 歌曲名 private string musicname; /** * 获取 主键id music_info.id * * @return 主键id */ public integer getid() { return id; } /** * 设置 主键id music_info.id * * @param id 主键id */ public void setid(integer id) { this.id = id; } /** * 获取 歌手名 music_info.singer_name * * @return 歌手名 */ public string getsingername() { return singername; } /** * 设置 歌手名 music_info.singer_name * * @param singername 歌手名 */ public void setsingername(string singername) { this.singername = singername == null ? null : singername.trim(); } /** * 获取 歌曲大小 music_info.music_size * * @return 歌曲大小 */ public string getmusicsize() { return musicsize; } /** * 设置 歌曲大小 music_info.music_size * * @param musicsize 歌曲大小 */ public void setmusicsize(string musicsize) { this.musicsize = musicsize == null ? null : musicsize.trim(); } /** * 获取 歌曲名 music_info.music_name * * @return 歌曲名 */ public string getmusicname() { return musicname; } /** * 设置 歌曲名 music_info.music_name * * @param musicname 歌曲名 */ public void setmusicname(string musicname) { this.musicname = musicname == null ? null : musicname.trim(); } @override public string tostring() { return "musicinfo{" + "id=" + id + ", singername='" + singername + '\'' + ", musicsize='" + musicsize + '\'' + ", musicname='" + musicname + '\'' + '}'; } }
创建mapper.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.chaoqi.springboot_test.dao.mapper.musicinfomapper"> <resultmap id="baseresultmap" type="com.chaoqi.springboot_test.dao.domain.musicinfo"> <id column="id" jdbctype="integer" property="id" /> <result column="singer_name" jdbctype="varchar" property="singername" /> <result column="music_size" jdbctype="varchar" property="musicsize" /> <result column="music_name" jdbctype="varchar" property="musicname" /> </resultmap> </mapper>
创建mapper
package com.chaoqi.springboot_test.dao.mapper; import com.chaoqi.springboot_test.dao.domain.musicinfo; import org.apache.ibatis.annotations.resultmap; import org.apache.ibatis.annotations.select; import java.util.list; public interface musicinfomapper { @resultmap("baseresultmap") @select("select * from music_info") list<musicinfo> selectall(musicinfo musicinfo); }
service接口
package com.chaoqi.springboot_test.service; import com.chaoqi.springboot_test.dao.domain.musicinfo; import java.util.list; public interface musicinfoservice { public list<musicinfo> getmusicinfo(musicinfo musicinfo); }
service实现类
package com.chaoqi.springboot_test.service.impl; import com.chaoqi.springboot_test.dao.domain.musicinfo; import com.chaoqi.springboot_test.dao.mapper.musicinfomapper; import com.chaoqi.springboot_test.service.musicinfoservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; @service public class musicinfoserviceimpl implements musicinfoservice { @autowired private musicinfomapper musicinfomapper; @override public list<musicinfo> getmusicinfo(musicinfo musicinfo) { list<musicinfo> musicinfos = musicinfomapper.selectall(null); return musicinfos; } }
创建完成后的结构如下
编辑indexcontroller
package com.chaoqi.springboot_test.web; import com.chaoqi.springboot_test.dao.domain.musicinfo; import com.chaoqi.springboot_test.service.musicinfoservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import java.util.list; @controller public class indexcontroller { private static final string index = "index"; @autowired private musicinfoservice musicinfoservice; @requestmapping("/show") public string getindex() { return index; } @requestmapping("/music") @responsebody public list<musicinfo> getmusicinfo(musicinfo musicinfo) { list<musicinfo> musicinfolist = musicinfoservice.getmusicinfo(null); return musicinfolist; } }
给springboottestapplication类加上注解@mapperscan("com.chaoqi.springboot_test.dao.mapper")
package com.chaoqi.springboot_test; import org.mybatis.spring.annotation.mapperscan; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @springbootapplication @mapperscan("com.chaoqi.springboot_test.dao.mapper") public class springboottestapplication { public static void main(string[] args) { springapplication.run(springboottestapplication.class, args); } }
运行项目,成功,springboot+springmvc+mybatis整合完成(源码下载地址)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
使用maven创建web项目的方法步骤(图文)
-
spring boot整合quartz实现多个定时任务的方法
-
spring boot整合CAS Client实现单点登陆验证的示例
-
IIS6+TOMCAT整合,实战实例!
-
Spring boot怎么整合Mybatis
-
zend studio怎么创建项目?zend studio创建并导入项目的方法
-
Eclipse下怎么创建Web项目?用Maven创建Web项目的教程
-
vue项目中使用tinymce编辑器的步骤详解
-
解决Maven 项目报错 java.httpservlet和synchronized使用方法
-
Spring整合多数据源实现动态切换的实例讲解