springboot 环境搭建
将之前博客开发内容转到springboot上,采用idea、springboot、mysql、mybatis、maven、git、springboot内置tomcate。这部分配置要比SpringMVC容易很多,随意搜索一个教程正确率都很高。
- [一.工具下载]
- [二.创建项目]
1.工具下载
java、idea、mysql、maven、git到相关网站下载一份,配置java环境变量,这部分就不说,随意找找就好。
二.创建项目
打开idea首相要进行java、maven、git(不用的话就不需要了,还是用用吧)配置。在新建页面有个小窗口直接进入setting,或者已用过file->setting。
1. git配置
根据图片找到下载的git就好了。
2.maven配置
这部分和之前写的博客maven配置一样,如下图,repository就是本地存储maven库,通过线上库下载的jar。注意一下setting的配置就好,找一个下载快的库,比如替换掉setting文件一部分
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
3.jdk配置
这部分建项目如果没有jdk会有提示,点出来,选择环境变量配置jdk就好。这idea还真心有点小智能。
4.springboot web项目创建(接近于没有配置)
(1)用file->new->project->Spring Initialzr->next,填写maven项目相关信息,然后next。
(2)选择需要的东西,在前面打对勾,这里只用了Web->web, sql->Mybatis、mysql,finished就完事了。
5.项目目录(按照之前博客写了点东西)
xml是自己创建出来的,建好项目后会自动生成一个BlogApplication的文件,这个就是springboot的入口main函数了。pom.xml就是maven导入的jar了,都是根据配置好的。application.properties是配置文件,这里配置相关属性。
6.pom.xml文件
根据需要加了druid连接池和fastjson的jar包,这两个是自己手动加上的。
<?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>cn.bjjoy.fblog.service</groupId>
<artifactId>blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>blog</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.application.properties和application-dev.properties文件
application.properties,springboot项目启动自动加载的文件,里边就写了一行,表明要加载application-dev.properties
spring.profiles.active=dev
application-dev.properties,这是项目需要的主配置,直接写到上面的application.properties文件也行。里边配置就像SpringMVC里边xml配置一样,springboot在properties里边配置就好了。
# 端口号
server.port=8010
# 驱动配置信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/fblog?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# 连接池的配置信息
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
mybatis.mapper-locations=classpath:mapper/*.xml
8.入口main函数文件BlogApplication
这个就是一个java,加的那两个注解就是加载了spring相关配置,和开启了事务。
package cn.bjjoy.fblog.service.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
9.人生一下感悟
上边是基本的配置,可以完成从数据库读取,到返回html页面数据。controller、service、dao(多了@Mapper)层注解都和之前博客写的一样,感觉比之前配置springmvc真是简化了好多。当然这只是基本功能,springboot用的技巧还有很多,自己也没研究太多。mybatis有一点点学习成本,这里给个dao和mybatis的xml就好,具体对应关系一看就知道了。(接口名对应xml里边查询语句的id,直接写sql语句也是可以的,这里用mybatis给的语法,感觉看着舒服点)
BloggerMapper.java接口文件
package cn.bjjoy.fblog.service.blog.dao;
import cn.bjjoy.fblog.service.blog.entity.Blogger;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Mapper
@Repository
public interface BloggerMapper {
List<Map> getBloggerList(Map param);
int insert(Map param);
int insertBlogger(Blogger blogger);
int update(Map param);
int insertBatch(List<Blogger> bloggerList);
}
BloggerMapper.xml,数据库操作mybatis文件
<?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="cn.bjjoy.fblog.service.blog.dao.BloggerMapper">
<resultMap id="BloggerResultMap" type="java.util.Map">
<id property="bloggerUuid" column="blogger_uuid" jdbcType="VARCHAR"/>
<result property="bloggerName" column="blogger_name" jdbcType="VARCHAR"/>
<result property="profession" column="profession" jdbcType="VARCHAR"/>
<result property="bloggerSex" column="blogger_sex" jdbcType="TINYINT"/>
<result property="regionCode" column="region_code" jdbcType="VARCHAR"/>
<result property="bloggerBrief" column="blogger_brief" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="VARCHAR"/>
<result property="updateTime" column="update_time" jdbcType="VARCHAR"/>
</resultMap>
<select id="getBloggerList" parameterType="java.util.Map" resultMap="BloggerResultMap">
select * from blogger
<where>
<if test="bloggerName != null">
blogger_name = #{bloggerName}
</if>
</where>
</select>
<insert id="insert" parameterType="java.util.Map">
insert into blogger
<trim prefix="(" suffix=")">
<if test="bloggerName != null">
blogger_name,
</if>
<if test="profession != null">
profession,
</if>
<if test="bloggerSex != null">
blogger_sex,
</if>
<if test="regionCode != null">
region_code,
</if>
<if test="bloggerBrief != null">
blogger_brief,
</if>
blogger_uuid
</trim>
<trim prefix="values (" suffix=")">
<if test="bloggerName != null">
#{bloggerName},
</if>
<if test="profession != null">
#{profession},
</if>
<if test="bloggerSex != null">
#{bloggerSex},
</if>
<if test="regionCode != null">
#{regionCode},
</if>
<if test="bloggerBrief != null">
#{bloggerBrief},
</if>
#{bloggerUuid}
</trim>
</insert>
<insert id="insertBlogger" parameterType="cn.bjjoy.fblog.service.blog.entity.Blogger">
insert into blogger
<trim prefix="(" suffix=")">
blogger_uuid,
blogger_name,
profession,
blogger_sex,
region_code,
blogger_brief
</trim>
<trim prefix="values (" suffix=")">
#{bloggerUuid},
#{bloggerName},
#{profession},
#{bloggerSex},
#{regionCode},
#{bloggerBrief}
</trim>
</insert>
<update id="update" parameterType="java.util.Map">
update blogger
<set>
<if test="bloggerName != null">
blogger_name = #{bloggerName},
</if>
<if test="profession != null">
profession = #{profession},
</if>
<if test="bloggerSex != null">
blogger_sex = #{bloggerSex},
</if>
<if test="regionCode != null">
region_code = #{regionCode},
</if>
<if test="bloggerBrief != null">
blogger_brief = #{bloggerBrief}
</if>
</set>
<where>
blogger_uuid = #{bloggerUuid}
</where>
</update>
<insert id="insertBatch" parameterType="java.util.List">
insert into blogger
<trim prefix="(" suffix=")">
blogger_uuid,
blogger_name,
profession,
blogger_sex,
region_code,
blogger_brief
</trim>
values
<foreach collection="list" item="item" separator=",">
(#{item.bloggerUuid},
#{item.bloggerName},
#{item.profession},
#{item.bloggerSex},
#{item.regionCode},
#{item.bloggerBrief})
</foreach>
</insert>
</mapper>