第一节 helloworld
springboot的环境配置
项目启动过程分析:
找到项目入口类,由@SpringBootApplication描述。然后运行启动类,检测启动过程,springboot启动时,控制台会出现标识。springboot项目启动时,首先会基于启动类上的注解描述,进行自动配置并扫描指定包及其子包的类进行加载。然后检测类上是否有Spring框架中指定的注解描述。假如有,则将类交给Spring框架中的BeanFactory工厂接口的实现类对象,此工厂对象会基于反射创建由注解描述的类的实例。假如此Bean指定了生命周期方法,还会调用生命周期方法。当实例创建以后,Spring框架还会基于类的作用域描述,将实例存储到不同作用域的容器中。
@SpringBootApplication
public class HelloWordApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWordApplication.class, args);
System.out.println("helloworld");
}
}
第二节 一个简单的业务
@Component
public class cache {
@Scope("singleton")
@Lazy
public void HelloCache() {
System.out.println("hellocache()");
}
@PostConstruct
public void init() {
System.out.println("init()");
}
@PreDestroy
public void destory() {
System.out.println("destory()");
}
}
@SpringBootTest
public class cacheTest {
@Autowired
private cache cache;
@Test
public void testCache() {
System.out.println(cache);
}
}
业务加强
package com.cy.cache;
public interface cacheInterface {
}
@Component
public class cache implements cacheInterface {
@Scope("singleton")
@Lazy
public void HelloCache() {
System.out.println("hellocache()");
}
@PostConstruct
public void init() {
System.out.println("init()");
}
@PreDestroy
public void destory() {
System.out.println("destory()");
}
}
@Component
public class secondcache implements cacheInterface {
}
@SpringBootTest
public class cacheTest {
@Autowired
@Qualifier("cache")
private cacheInterface cacheInterface;
@Test
public void testCache() {
System.out.println(cacheInterface);
}
}
第三节
springboot整合连接池
整合HikariCP连接池,springboot工程默认推荐使用HiKariCP连接池
springboot整合mybaits
springboot整合springmvc
练习:
封装数据
public class Goods {
private Long id;
private String name;
private String remark;
private Date createdTime;
前端,goods页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>this is the goods page</h1>
<div>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>remark</th>
<th>createdTime</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr th:each="g:${list}">
<td th:text="${g.id}"></td>
<td th:text="${g.name}"></td>
<td th:text="${g.remark}"></td>
<td th:text="${g.createdTime}"></td>
<td ><a th:href="@{/goods/dodeletebyid(id=${g.id})}">delete</a>></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
练习一: 将数据库中的商品数据查询出来更新到页面上
dao
@Mapper
public interface GoodsDao {
@Select("select * from tb_goods")
List<Goods> findObjects();
}
service
public interface GoodsService {
List<Goods> findGoods();
}
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsDao goodsDao;
@Override
public List<Goods> findGoods() {
// TODO Auto-generated method stub
List<Goods> list=goodsDao.findObjects();
return list;
}
}
controller
@Controller
@RequestMapping("/goods/")
public class GoodsControl {
@Autowired
private GoodsService goodsService;
@RequestMapping("doGoodsUI")
public String doGoodsUI(Model model) {
List <Goods>list=goodsService.findGoods();
model.addAttribute("list",list);
return "goods";
}
}
练习二:基于ID删商品库中的商品信息
dao
@Delete("delete from tb_goods where id=#{id}")
int deleteById(Integer id);
service
int deleteid(Integer id);
@Override
public int deleteid(Integer id) {
// TODO Auto-generated method stub
int rows=goodsDao.deleteById(id);
return rows;
}
controller
@RequestMapping("dodeletebyid")
public String dodeletebyid(Integer id) {
goodsService.deleteid(id);
return "redirect:doGoodsUI";
}
练习三:将页面用户输入的商品信息写入到数据库
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>this is the goods page</h1>
<div>
<form action="doSaveGoods" method="post">
<ul>
<li>商品名称:
<li><input type="text" name="name" placeholder="请输入商品名称">
<li>商品描述:
<li><textarea rows="3" cols="50" name="remark" placeholder="请输入商品名称"></textarea>
<li><button type="submit">保存</button>
</ul>
</form>
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>remark</th>
<th>createdTime</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr th:each="g:${list}">
<td th:text="${g.id}"></td>
<td th:text="${g.name}"></td>
<td th:text="${g.remark}"></td>
<td th:text="${g.createdTime}"></td>
<td ><a th:href="@{/goods/dodeletebyid(id=${g.id})}">delete</a>></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
dao
@Insert("insert into tb_goods(name,remark,createdTime)values(#{name},#{remark},now())")
int insertGoods(Goods entity);
service
int saveGoods(Goods entity);
@Override
public int saveGoods(Goods entity) {
int rows=goodsDao.insertGoods(entity);
return rows;
}
controller
@RequestMapping("doSaveGoods")
public String doSaveGoods(Goods entity) {
int rows=goodsService.saveGoods(entity);
return "redirect:doGoodsUI";
}
mybaits的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.cy.goods.testDelete">
<delete id="deleteObjects">
delete from tb_goods
<where>
id in
<foreach collection="ids"
open="("
close=")"
separator=","
item="id">
#{id}
</foreach>
</where>
</delete>
</mapper>
springboot的配置文件
```java
spring.datasource.url=jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
logging.level.com.cy=DEBUG
mybatis.mapper-locations=classpath:/mappergoods/GoodsMapper.xml
spring.thymeleaf.prefix=classpath:/templates/pages/
spring.thymeleaf.suffix=.html
第四节 简单的增删改查项目
准备工作
pojo
public class activity {
private Long id;
private String title;
private String category;
private Date startTime;
private Date endTime;
private String remark;
private Long state;
private Date createdTime;
private String createdUser;
前端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<form action="doActivityInsert" method="post">
<ul>
<li>活动标题:
<li><input type="text" name="title" placeholder="请输入活动标题">
<li>活动种类:
<li><input type="text" name="category" placeholder="请输入活动种类">
<li>活动意义:
<li><input type="text" name="remark" placeholder="请输入活动意义">
<li>活动状态:
<li><input type="text" name="state" placeholder="请输入活动状态">
<li>活动创始人:
<li><input type="text" name="createdUser" placeholder="请输入活动创始人">
<li><button type="submit">保存</button>
</ul>
</form>
<table>
<thead>
<tr>
<th>id</th>
<th>title</th>
<th>category</th>
<th>startTime</th>
<th>endTime</th>
<th>remark</th>
<th>state</th>
<th>createdTime</th>
<th>createdUser</th>
</tr>
</thead>
<tbody>
<tr th:each="g:${list}">
<td th:text="${g.id}"></td>
<td th:text="${g.title}"></td>
<td th:text="${g.category}"></td>
<td th:text="${g.startTime}"></td>
<td th:text="${g.endTime}"></td>
<td th:text="${g.remark}"></td>
<td th:text="${g.state}"></td>
<td th:text="${g.createdTime}"></td>
<td th:text="${g.createdUser}"></td>
<td ><a th:href="@{/activity/doActivityDelete(id=${g.id})}">delete</a>></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
dao
@Mapper
public interface activityDao {
@Insert(" insert into tb_activity\r\n" +
" (title,category,startTime,endTime,remark,state,createdUser,createdTime)\r\n" +
" values\r\n" +
" (#{title},#{category},now(),now(), #{remark},#{state},#{createdUser},now())")
int insertActivity(activity entity);
@Delete("delete from tb_activity where id=#{id}")
int deleteById(Integer id);
// @Update("")
// int updateActivity;
@Select("select * from tb_activity")
List<activity> findObjects();
}
service
public interface ActivityService {
List<activity> findActivity();
int deleteid(Integer id);
int insertac(activity entry);
}
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private activityDao activityDaos;
@Override
public List<activity> findActivity() {
// TODO Auto-generated method stub
List<activity> list=activityDaos.findObjects();
return list;
}
@Override
public int deleteid(Integer id) {
int rows=activityDaos.deleteById(id);
return rows;
}
@Override
public int insertac(activity entry) {
int rows =activityDaos.insertActivity(entry);
return rows;
}
}
controller
@Controller
@RequestMapping("/activity/")
public class ActivityControl {
@Autowired
private ActivityService activityService;
@RequestMapping("doActivityUI")
public String doActivityUI(Model model) {
List <activity>list=activityService.findActivity();
model.addAttribute("list",list);
return "activity";
}
@RequestMapping("doActivityDelete")
public String doActivityDelete(Integer id) {
activityService.deleteid(id);
return "redirect:doActivityUI";
}
@RequestMapping("doActivityInsert")
public String doActivityInsert(activity entry) {
activityService.insertac(entry);
return "redirect:doActivityUI";
}
}
第5节 json
@RequestMapping("dofindactivitys")
@ResponseBody
public List<activity>dofindactivitys(){
return activityService.findActivity();
}
客户端获取json数据
第6节 Spring Boot 应用加强实现
健康检查
热部署
Lombok插件应用