
第一节 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页面
Insert title here this is the goods page
id name remark createdTime operation delete>
练习一: 将数据库中的商品数据查询出来更新到页面上
dao
@Mapper
public interface GoodsDao {
@Select("select * from tb_goods")
List findObjects();
}
service
public interface GoodsService {
List findGoods();
}
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsDao goodsDao;
@Override
public List findGoods() {
// TODO Auto-generated method stub
List list=goodsDao.findObjects();
return list;
}
}
controller
@Controller
@RequestMapping("/goods/")
public class GoodsControl {
@Autowired
private GoodsService goodsService;
@RequestMapping("doGoodsUI")
public String doGoodsUI(Model model) {
List 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";
}
练习三:将页面用户输入的商品信息写入到数据库
Insert title here this is the goods page
id name remark createdTime operation delete>
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文件
delete from tb_goods id in #{id}
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;
前端页面
Insert title here
id title category startTime endTime remark state createdTime createdUser delete>
dao
@Mapper
public interface activityDao {
@Insert(" insert into tb_activityrn" +
" (title,category,startTime,endTime,remark,state,createdUser,createdTime)rn" +
" valuesrn" +
" (#{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 findObjects();
}
service
public interface ActivityService {
List findActivity();
int deleteid(Integer id);
int insertac(activity entry);
}
@Service
public class ActivityServiceImpl implements ActivityService {
@Autowired
private activityDao activityDaos;
@Override
public List findActivity() {
// TODO Auto-generated method stub
List 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 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 Listdofindactivitys(){
return activityService.findActivity();
}
客户端获取json数据
第6节 Spring Boot 应用加强实现
健康检查
热部署
Lombok插件应用