Springboot第一个项目
一、创建Springboot项目
1、菜单 -> New -> module -> Spring Initializer 然后点 Next
2、项目参数:GroupId与ArtifactId
3、选择Web模块:接着左边选择 Web, 右边只勾选 Web 即可,然后点击Next
4、指定项目路径;
5、SpringbootApplication.java,项目创建好之后,就自带一个SpringbootApplication, 其被@SpringBootApplication 所标记,表示这个是一个Springboot 应用;
6、构建package:com.lzz.springboot
然后新建控制器HelloController;
@RestController 是spring4里的新注解,是@ResponseBody和@Controller的缩写。
二、用 JAR的方式运行Springboot项目
1、通常来说,Springboot 部署会采用两种方式:全部打包成一个jar,或者打包成一个war。
2、进入该项目中包含pom.xml文件的文件夹路径 cd : D:\IdeWorkSpace\spring-boot-examples-master\springboot
然后执行mvn -package
将该项目打包;这里可以看到在项目的target目录下生成了一个jar,然后执行命令java -jar target/springboot-0.0.1-SNAPSHOT.jar
就可以启动着jar
三、用war包部署Springboot项目
1、修改Application代码:新加@ServletComponentScan注解,并且继承SpringBootServletInitializer
2、修改pom文件:<packaging>war</packaging>
3、执行命令:mvn clean package
生成war包
4、重命名war包,然后将其放入tomcat的webapps下部署;再执行命令startup
启动tomcat。
5、根据war包的名字,进行访问。
四、Springboot配置jsp视图支持
1、添加jsp依赖:
<!--jsp页面使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
2、修改部署的package:<packaging>war</packaging>
3、在src/main下新建目录webapp/WEB-INF/jsp
4、修改视图解析前后缀: spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
5、修改注解@RestController
为@Controller
6、支持jsp进行部署时只能打包成war;部署方式参照war包的部署方式。
五、springboot整合mybatis
1、添加Maven依赖;
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
2、Application.properties配置
#mybatis配置文件
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
#mybatis包的别名
mybatis.type-aliases-package=com.lzz.entity
#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_mybatis?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=******
3、mybatis-config.xml配置
在resources目录下创建mybatis/mybatis-config.xml
,配置如下:这里主要配置了常用数据类型的别名
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>
4、在数据库中创建表,这里是admins
主键设置自增长
5、创建Admin实体类
package com.lzz.entity;
public class Admin {
private Long id;
private String admin_name;
private String admin_password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAdmin_name() {
return admin_name;
}
public void setAdmin_name(String admin_name) {
this.admin_name = admin_name;
}
public String getAdmin_password() {
return admin_password;
}
public void setAdmin_password(String admin_password) {
this.admin_password = admin_password;
}
}
6、创建数据持久层接口:AdminMapper
package com.lzz.mapper;
import com.lzz.entity.Admin;
import org.springframework.stereotype.Service;
@Service("adminMapper")
public interface AdminMapper {
int deleteByPrimaryKey(Long id);
int insert(Admin record);
int insertSelective(Admin record);
Admin selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Admin record);
int updateByPrimaryKey(Admin record);
}
7、配置AdminMapper.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.lzz.mapper.AdminMapper" >
<resultMap id="BaseResultMap" type="com.lzz.entity.Admin" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="admin_name" property="admin_name" jdbcType="VARCHAR" />
<result column="admin_password" property="admin_password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, admin_name, admin_password
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from admins
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from admins
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.lzz.entity.Admin" >
insert into admins (id, admin_name, admin_password
)
values (#{id,jdbcType=BIGINT}, #{admin_name,jdbcType=VARCHAR}, #{admin_password,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.lzz.entity.Admin" >
insert into admins
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="admin_name != null" >
admin_name,
</if>
<if test="admin_password != null" >
admin_password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="admin_name != null" >
#{admin_name,jdbcType=VARCHAR},
</if>
<if test="admin_password != null" >
#{admin_password,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lzz.entity.Admin" >
update admins
<set >
<if test="admin_name != null" >
admin_name = #{admin_name,jdbcType=VARCHAR},
</if>
<if test="admin_password != null" >
admin_password = #{admin_password,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.lzz.entity.Admin" >
update admins
set admin_name = #{admin_name,jdbcType=VARCHAR},
admin_password = #{admin_password,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
8、创建AdminController
package com.lzz.controller;
import com.lzz.entity.Admin;
import com.lzz.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AdminController {
@Autowired private AdminMapper adminMapper;
@RequestMapping("/getAdmin/{id}")
public Admin getAdmin(@PathVariable("id") Long id){
return adminMapper.selectByPrimaryKey(id);
}
}
9、访问:http://localhost:8080/getAdmin/1
六、mybatis的generator代码生成
1、添加依赖;
<!--mybatis的generator依赖-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
2、添加generator插件
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
3、resources/generator/generatorConfig.xml下配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<properties resource="application.properties"/>
<!--连接数据库的jar-->
<classPathEntry
location="D:\Program Files\Maven_Repo\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/>
<!-- 一个数据库一个context -->
<context id="context1" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc连接-->
<jdbcConnection
driverClass="${spring.datasource.driver-class-name}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}"/>
<!--生成的java实体类-->
<javaModelGenerator targetPackage="com.lzz.entity"
targetProject="src/main/java"/>
<!--生成的mapper文件-->
<sqlMapGenerator targetPackage="mybatis/mapper"
targetProject="src/main/resources"/>
<!--生成的mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lzz.mapper"
targetProject="src/main/java">
</javaClientGenerator>
<!--对象关系模型映射-->
<table tableName="admins" domainObjectName="Admin"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true"/>
</table>
</context>
</generatorConfiguration>
4、在util下:新建generatorMain类
package com.lzz.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.util.ResourceUtils;
public class GeneratorMain {
public static void main(String[] args) throws FileNotFoundException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//读取配置文件
/*File configFile = new File(ResourceUtils.getURL("classpath:").getPath() + "/generator/generatorConfig.xml");*/
File configFile = new File("src/main/resources/generator/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config;
try {
config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
//打印结果
for (String str : warnings) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
}
}
5、执行即可生成数据持久层相关代码
上一篇: css3边框和圆角
下一篇: 微信新上线流量入口:“热议话题”