提示:springBoot默认的支持的模版是Thymeleaf,因此使用起来Thtymeleaf相比较于其他的方式会简单一点
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Controller
Controller在springboot自己编写的部分中是第一阶级,通过以mapping找到需要使用的Controller。Controller的实现类中会有Service的实体类属性,成为和Service的连接,通过AutoWired自动配置得到
@RequestMapping("/")
public String index() {
return "redirect:/list";
}
@RequestMapping("/list")
public String list(Model model) {
System.out.println("查询所有");
List<User> users=userService.findAll();
model.addAttribute("users", users);
return "user/list";
}
映射过程中,根据返回的字符串跳转到指定的页面,RequestMapping用于解析返回的参数。“redirect:”表示重定向,转到@RequestMapping("/list")注释的函数,最终跳转到resources/templates目录下user/list
二、Service+Dao
@Override
public boolean addUser(User user) {
boolean flag=false;
try{
userDao.addUser(user);
flag=true;
}catch(Exception e){
System.out.println("新增失败!");
e.printStackTrace();
}
return flag;
}
以添加用户为例,Controller层调用userservice的添加用户,进而去调用永久层(Dao)与数据库进行交互。
@Mapper
public interface UserDao {
@Insert("insert into t_user(id,name,password,age) values (#{id},#{name},#{password},#{age})")
void addUser(User user);
@Update("update t_user set name=#{name},password=#{password}, age=#{age} where id=#{id}")
void updateUser(User user);
@Delete("delete from t_user where id=#{id}")
void deleteUser(int id);
@Select("SELECT * FROM t_user where id=#{id}")
User findById(int id);
@Select("SELECT * FROM t_user")
List<User> findAll();
}
这部分就类似Mybatis的内容,通过注解执行SQL语句
运行时
可以看到运行程序之后启动tomcat容器,开启Service和servlet,对多个过滤器进行了初始化(或者是启动?)
server.port=8085
spring.datasource.url=jdbc:mysql://localhost:3306/springBoot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
根据配置端口号为8085,表名名为springboot,通过在web输入localhost:8085/…即可进行相应的增删改查等操作,
总结
这是springboot较为简单的操作了,毕竟我这个渣渣也能看懂,不过麻雀虽小,五脏俱全,结合了MVC、Mybatis、Thymeleaf模板渲染,可以实现简单的网页制作。关于前端的内容我没有深入了解,但是用到了Css对html文件进行了加工。(层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS不仅可以静态地修饰网页,还可以配合各种脚本语言动态地对网页各元素进行格式化。)