基础目录
JDK下载登录解决:账号提供 http://bugmenot.com/view/oracle.com
一、新建项目
1.初始创建
初始创建不多说,选项中如果使用MVC 则必选 Spring Web.
注意:必须在配置文件中指明端口。
2.SpringMVC的一些细节
(1)简单接口的实现:
创建新的控制类 使用注解@Controlled 注明
新建方法 使用@RequestMapping(“/”) 注明访问路径
注意:必须加@ResponseBody,否则404
PS:加上它代表直接把值return到页面,不加则找return对应的文件。如:return “hi”,就是去找hi.html
(2)读取传值的方法:
如果传值封装在data,则在方法中声明同名形参即可。
如果为result风格,使用以下两种
@RequestParam
格式如:localhost:8080/test?Value=10&Key=11
声明变量为@RequestParam(“值名”) 类型名 变量名
@ PathVariable
必须在@RequestMapping指明格式如“/test/{id}“
URL:localhost:8080/test/10
@PathVarianle(“指明格式的变量名”) 类型 变量名
(原文)
如果是post方法
使用
@RequestBody Map<String,String> Valuename
这样会把data放在一个Map中,方便读取。
@RequestBody @Validated 类 类名 直接匹配放到类里
3.Model 和 ModelAndView
介绍文章:https://blog.csdn.net/swebin/article/details/90416547
二、整合Mybatis
1.初始化
在这一步主要需要检查是否以经完成了引入否则需要在pom.xml中引入Mybatis、mysql
大致如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2.重要的建议
在pom.xml中一定要注意配置以下信息,以减小麻烦。
放在build中
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<!--这是ssl证书在打包后还存在的重要依据-->
<include>**/*.jks</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
3.模型类
模型类最好放在模型的包中,它是个抽象类,类名最好同数据库表名一致
,在类中声明的变量要和表字段类型相同,如果不好判断可以使用String
代码略
4.新建mapper接口和mapper.xml
新建mapper包,新建mapper接口,接口就等同于表示你有的对数据库操作方法的声明。在其中写方法 格式: 返回值类型 方法名由于是接口不必写具体。
在这一步最好在类前加入以下两个标签
@Mapper
@Repository
在同级目录下新建Mapper.xml文件
在这个文件下,主要配置的就是Mybatis比较核心的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="xyz.liontt.backend.mapper.projeMapper">
<select id="selectAll" resultType="xyz.liontt.backend.model.projectls">
select * from projectls;
</select>
<select id="selecttab" resultType="xyz.liontt.backend.model.tab">
select * from tab;
</select>
</mapper>
其中 namespce指向之前新建的接口,resultType指向模型类(查询全部返回是列表所以指向模型类,否则可以自定义为int或String),id要和接口中的方法名一致
在Springboot主类前加入标签
@MapperScan(basePackages = “xyz.liontt.backend.mapper”)
basePackages指向mapper类和文件所在的包(package)
配置application.properties如下,主要注释中的内容
#端口号和http端口号
server.port=3000
server.http-port=3000
#https的协议信息
server.ssl.key-store: classpath:/Tomcat/liontt.xyz.jks #jks文件目录
server.ssl.key-store-password: ***** #密码
#数据库相关配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=classpath:xyz.liontt.backend.model
# 重点 这一步配置不好会报Invalid bound statement (not found)
mybatis.mapper-locations=classpath:/xyz/liontt/backend/mapper/*Mapper.xml
# 重点
#mysql8.0 url
#spring.datasource.url=jdbc:mysql://localhost:3306/lionttmain?characterEncoding=UTF-8&serverTimezone=UTC
#mysql 5.0 url 服务器使用5.0
spring.datasource.url=jdbc:mysql://localhost:3306/lionttmain?useSSL=false
#数据库用户名
spring.datasource.username=root
#数据库密码
spring.datasource.password=****
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
5.新建service类
首先先建包,后建类,使用注解注明@Service
在下面实例化Mapper接口,记得自动注入@Autowired
之后写一个方法,返回值调用实例化接口后的方法。
代码例子:
@Service
public class tabService {
@Autowired
public projeMapper pro;//projeMapper是一个接口
public List<tab> selecttab(){ //返回值是List参数是调用模型类
return pro.selecttab();//使用接口中的方法,会自动去xml找匹配
}
}
6.创建Controller
使用注解
@RestController 相当于ResponseBody+Controller
@CrossOrigin 解决跨域问题
在类中
实例化Service类,记得自动注入
@Autowired //或@Resource,记得自己搞懂
public tabService tabS;
之后实现方法,并调用实例化类中的方法
@RequestMapping(value = "/tab")
public List<tab> tab(){
return tabS.selecttab();
}
配置完毕。
三、线上部署
线上部署要格外精细,要注意服务器的Mysql版本,选择URL
也要注意pom.xml对打包文件的要求,不要漏了配置文件。
1.maven打包文件
先clean清理target,在点击package
打包完成后会在target下生成一个.jar文件,你需要把它上传到服务器tomcat服务器下的webapps中(其实不必,但是为了好整理)
2.执行文件
在命令行中进入到上传的目录,使用java -jar 包名.jar 进行执行,如果没有报错则说明完成。
使用 nohup java -jar XXX.jar > Log.log & 会在后台执行这个指令,这样关闭命令行就不会终止了,日志放在Log.log中。
3.https的实现
四、MyBatis-Plus(CRUD)
https://mybatis.plus/guide/crud-interface.html
1.配置pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
2.配置application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/book?characterEncoding=UTF-8&serverTimezone=UTC
server:
port: 3000
mybatis-plus:
mapper-locations: classpath:com/liontt/mydemo/mapper/*.xml
3.创建实体类
@Data
@TableName("booklist")
public class booklist {
@TableId(value = "bookid",type = IdType.AUTO)
private int bookid;
@TableField("bookname")
private String bookname;
@TableField("bookdetail")
private String bookdetail;
}
4.创建Mapper接口
@Mapper
//继承BaseMapper,这是MybatisPlus的基础Mapper,定义了一些常用的方法
public interface mymapper extends BaseMapper<booklist> {
//自定义方法
@Delete("Delete from booklist where bookid = #{id};")
int deleteByNameId(@Param("id") int id);
}
5.业务层定义
@Service
public class search {
@Autowired
private mymapper my;
public List<booklist> searchall(){
return my.selectList(null);
}
}
CUD略过
控制层定义
@RestController
@CrossOrigin //跨域
public class mycontroll {
@Autowired
private search select;
@RequestMapping("/search")
public List<booklist> search(){
return select.searchall();
}
@Autowired
private del del;
@PostMapping(value = "/del")
public int del(@RequestBody @Validated booklist bk){
int id = bk.getBookid();
int code=del.del(bk);
return code;
}
}
以上就是一个简单的MybatisPlust的demo例子,具体的可以参考文档。