SpringBoot简介
Spring Boot 是所有基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。简单来说就是SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。
SpringBoot四个主要特性:
1、SpringBoot Starter:他将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中;
2、自动配置:SpringBoot的自动配置特性利用了Spring4对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们;
3、命令行接口:(Command-line-interface, CLI):SpringBoot的CLI发挥了Groovy编程语言的优势,并结合自动配置进一步简化Spring应用的开发;
4、Actuatir:它为SpringBoot应用的所有特性构建一个小型的应用程序。但首先,我们快速了解每项特性,更好的体验他们如何简化Spring编程模型。
SpringBoot开发的具体好处:
1、配置web.xml,加载spring和spring mvc
2、配置数据库连接、配置spring事务
3、配置加载配置文件的读取,开启注解
导入依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wn</groupId> <artifactId>springbootproject02</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootproject02</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- 核心依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 可以实现热部署,在IDEA上实现热部署还需一些额外的配置,请查阅资料 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>runtime</scope> </dependency> <!-- JDBC for mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis --> <!--mybatis--> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--fastJson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--thymeleaf 新的模板引擎,比jsp要出色--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!--xml配置,此是为了将来整合Hibernate或者mybatis 默认没有需要配置 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
Users实体类:
package com.wn.springbootproject02.entity; public class Users { private Integer uid;private String userName; private String password; private String realName; public Users(Integer uid, String username, String password, String realname) { this.uid = uid; this.userName = username; this.password = password; this.realName = realname; } public Users() { super(); } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } }
dao层接口
@Repository("iUserDao") public interface IUserDao{ public Users login(@Param("username") String username, @Param("userpassword") String userpassword); }
dao层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指定Dao接口的完整类名 mybatis会依据这个接口动态创建一个实现类去实现这个接口, 而这个实现类是一个Mapper对象--> <mapper namespace="com.wn.springbootproject02.dao.IUserDao"> <resultMap id="MapList" type="com.wn.springbootproject02.entity.Sale"> <id property="sid" column="sid"></id> <result property="price" column="price"></result> <result property="quantity" column="quantity"></result> <result property="totalPrice" column="totalPrice"></result> <result property="saleDate" column="saleDate"></result> <result property="userId" column="userId"></result> <result property="productId" column="productId"></result> <association property="users" javaType="com.wn.springbootproject02.entity.Users"> <id property="uid" column="uid"></id> <result property="userName" column="userName"></result> <result property="password" column="password"></result> <result property="realName" column="realName"></result> </association> <association property="product" javaType="com.wn.springbootproject02.entity.Product"> <id property="pid" column="pid"></id> <result property="productName" column="productName"></result> <result property="quantity" column="quantity"></result> </association> </resultMap> <!--登录--> <select id="login" resultType="com.wn.springbootproject02.entity.Users"> SELECT * FROM users WHERE username=#{username} AND PASSWORD=#{userpassword} </select> </mapper>
UserService接口层
public interface IUserService { public Users login(String username, String userpassword); }
UserService接口实现层
@Service("iUserService") public class IUserServiceImpl implements IUserService { //植入dao层对象 @Resource(name = "iUserDao") private IUserDao dao; @Override public Users login(String username, String userpassword) { return dao.login(username,userpassword); } }
UserController层
@Controller @RequestMapping("/user") public class IUserController { //植入对象 @Resource(name = "iUserService") private IUserService service; /*返回頁面*/ @RequestMapping("/getlogin") public String getlogin(){ return "login"; } /*登录*/ @RequestMapping("/login") public ModelAndView login(Users user, ModelAndView mv, HttpServletRequest request, Model model){ Users login = service.login(user.getUserName(),user.getPassword()); System.out.println(login); if (login!=null){ request.getSession().setAttribute("login",login); System.out.println("成功!!"); mv.setViewName("index"); }else{ System.out.println("失败!!"); mv.setViewName("login"); } return mv; } }
application.properties文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///invoicingsystem spring.datasource.username=root spring.datasource.password=123 #Spring Data JPA配置 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent-output=true spring.jpa.database=mysql spring.main.allow-bean-definition-overriding=true
login.html页面
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>系统登录 - 小型进销存系统</title> <link rel="stylesheet" href="css/style.css"/> <style> #parent{ width:500px; height:200px; margin-top:20%; margin-left:50%; transform:translate(-50%,-50%) ; background:#009688; } .password,.subBtn{ margin-top: 2%; margin-left: 3%; } .loginHeader{ padding-top: 1%; } </style> </head> <body class="login_bg"> <div id="parent"> <section class="loginBox"> <header class="loginHeader" style="text-align:center; "> <h1>小型进销存系统</h1> </header> <section class="loginCont"> <form class="loginForm" action="/user/login" method="post" onsubmit="return check()" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <input id="user" type="text" name="userName" placeholder="请输入用户名" required="required" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" required="required" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form> </section> </section> </div> </body> </html>
index.html页面
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en" > <meta charset="UTF-8"> <title>小型进销存系统</title> <!-- <link rel="stylesheet" th:href="@{/css/public.css}"> <link rel="stylesheet" th:href="@{/css/style.css}">--> <style> #parent{ margin-top:13%; margin-left:45%; transform:translate(-50%,-50%) ; } </style> </head> <body> <div id="parent"> <!--头部--> <header class="publicHeader"> <div class="publicHeaderR"> <p><span></span><span style="color: red" th:text="${session.login.getRealName()}"></span> , 欢迎你! <a href="/user/remover">退出登录</a></p> </div> </header> <!--主体内容--> <section class="publicMian"> <div class="left"> <nav> <ul class="list"> <li ><a href="/user/prodectAdd">销售</a></li> <li><a href="/user/saleList">销售信息查询</a></li> <li><a href="/user/view">查看库存</a></li> </ul> </nav> </div> <div class="right" style="border: 3px solid blue;width: 400px;height: 200px;margin-top: -100px;margin-left: 200px" > <img class="wColck" src="img/clock.jpg" alt=""/> <div class="wFont" style="text-align:center;margin-top:10% "> <p>欢迎使用小型进销存系统系统!</p> </div> </div> </section> </div> </body> </html>