introduce:
In the previous article, I introduced the construction of the SSM multi-module project. During the construction process, Spring integrates springmvc and mybatis, there will be many things that we need to configure. This not only wastes time, but also makes mistakes more prone to errors. Due to the problems caused, the Pivotal team provided a brand new framework, which uses a specific way to configure, so that developers no longer need to define boilerplate configurations. In this way, Spring Boot is committed to becoming a leader in the booming rapid application development.
Features:
1. Create a standalone Spring application
2. Embed Tomcat, no need to deploy WAR files
3. Simplify Maven configuration
4. Automatically configure Spring
5. Provide production-ready features such as metrics, health checks and external configurations
6. There is absolutely no code generation and no requirements for XML configuration
(Most of the above content is excerpted from Baidu Encyclopedia)
OK, this is all for the explanation of springboot. If you want to know more in detail, you can use Baidu.
I recommend you to build a springboot project using ideas or sts (an editor developed by spring tool suite spring company). I am now using ideas, so next I use ideas to build the project.
1. Create a project
After filling in Group and Atrifact, click Next. Here I chose jar. Because the official document recommends packaging into JAR, I won’t explain much here.
Check the web here
Here, check MySQL, JDBC and Mybatis to click Next
Enter the project name and project path here and click Finish
This is the project structure after the new construction is completed
2. Add pom.xml dependency
Because springboot does not recommend using jsp as pages, if you want to use it, you must add jsp dependencies.
<?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> <!--Add jsp dependencies--> <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>
3. Springboot integrates springmvc
Because when creating the project, we chose mybatis and jdbc, so we have to configure them here.
Edit application.properties
# The default prefix directory of page spring.mvc.view.prefix=/WEB-INF/jsp/# The default suffix of response page spring.mvc.view.suffix=.jsp#Development configuration spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456# mybatis interface file location mybatis.mapper-locations: classpath:mapper/*.xmlmybatis.type-aliases-package: com.chaoqi.springboot_demo2.domain
If you are used to using application.yml, it can also be used, but when using application.yml, you must maven clean it for the first time, otherwise an error will be reported.
server: port: 8080spring: 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.Drivermybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.chaoqi.springboot_demo2.domain
After editing application.properties, create the webapp directory under src/mian. The structure is as follows
Create a new IndexController
package com.chaoqi.springboot_test.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController { private static final String INDEX = "index"; @RequestMapping("/show") public String getIndex() { return INDEX; }}Run the main function
Visit the page, successful
4. Springboot integrates mybatis
Create a database table
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- `music_name` varchar(100) NOT NULL COMMENT 'Song title', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 'Andy Lau', '3.0M', 'Forgetful Water');INSERT INTO `music_info` VALUES ('3', 'Pig Diandian', '5.0M', 'Little Pig who can write programs');Create a pojo
package com.chaoqi.springboot_test.dao.domain;public class MusicInfo { // primary key id private Integer id; // singer name private String singerName; // song size private String musicSize; // song name private String musicName; /** * Get primary key id music_info.id * * @return primary key id */ public Integer getId() { return id; } /** * Set primary key id music_info.id * * @param id primary key id */ public void setId(Integer id) { this.id = id; } /** * Get the singer name music_info.singer_name * * @return singer name*/ public String getSingerName() { return singerName; } /** * Set singer name music_info.singer_name * * @param singerName SingerName */ public void setSingerName(String singerName) { this.singerName = singerName == null ? null : singerName.trim(); } /** * Get song size music_info.music_size * * @return song size*/ public String getMusicSize() { return musicSize; } /** * Set song size music_info.music_size * * @param musicSize Song size */ public void setMusicSize(String musicSize) { this.musicSize = musicSize == null ? null : musicSize.trim(); } /** * Get song name music_info.music_name * * @return song name */ public String getMusicName() { return musicName; } /** * Set the song name music_info.music_name * * @param musicName Song name */ 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 + '/'' + '}'; }}Create mappper.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>
Create a 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 interface
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 implementation class
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;@Servicepublic class MusicInfoServiceImpl implements MusicInfoService { @Autowired private MusicInfoMapper musicInfoMapper; @Override public List<MusicInfo> getMusicInfo(MusicInfo musicInfo) { List<MusicInfo> musicInfos = musicInfoMapper.selectAll(null); return musicInfos; }}The structure after creation is as follows
Edit 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;@Controllerpublic 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; }}Annotate the SpringbootTestApplication class @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); }}Run the project successfully, springboot+springmvc+mybatis integration is completed (source code download address)
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.