1.使用Spring Initializer快速创建Spring Boot项目
1.1 IDEA:使用 Spring Initializer快速创建项目
IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;
选择我们需要的模块;向导会联网创建Spring Boot项目;
默认生成的Spring Boot项目;
- 主程序已经生成好了,我们只需要我们自己的逻辑
- resources文件夹中目录结构
1. static:保存所有的静态资源; js css images;
2. templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板(freemarker、thymeleaf);
3. application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;
导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--引入springboot-mybatis的依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--MySQL的依赖--> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
实体类
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("iUsersDao") public interface IUsersDao { //登录 Users getlogin(@Param("userName") String userName, @Param("password") String password); }
dao层xml
<mapper namespace="com.example.dao.IUsersDao"> <!--登录--> <select id="getlogin" resultType="com.example.entity.Users"> SELECT * FROM users WHERE userName=#{userName} AND PASSWORD=#{password} </select> </mapper>
IUserService接口层
public interface IUsersService { //登录 Users getlogin(String userName, String password); }
IUserService接口实现层
@Service("iUsersService") public class IUsersServiceImpl implements IUsersService { @Resource("iUsersDao") private IUsersDao iUsersDao; @Override public Users getlogin(String userName, String password) { return iUsersDao.getlogin(userName,password); } }
IUserController层
@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=root
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>