SpringBoot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
SpringBoot特性
1. SpringBoot并不是对Spring功能上的增强,而是提供了一种快速创建独立的Spring应用程序的框架
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 绝对没有代码生成和对XML没有要求配置
6.备受关注,是下一代框架,已经是不争的事实,不需要学习springmvc
7.微服务的入门级微框架,springboot是springcloud的基础
结构
导入依赖
<?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{ 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; 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>
实现