赞
踩
采用SpringBoot搭建项目的后台,完整项目结构如下
结构说明:
bean:里面书写自己的实体类
controller:里面书写和前端交互的路由信息
dao:里面书写接口信息
util:自己的工具类
也可以添加config,可以配置swagger,测试自己的接口是否配置正确
详细配置:
application.yml
在resource/application.yml里面配置数据源与整合mybatis的内容以及端口号
#mysql
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/sportplay?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
#mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zut.sportplay.bean
server:
port: 9000
bean省略get,set方法
user:
public class User {
private int id;
private String username;
private String password;
private String email;
private String role;
private Boolean state;
}
MainMenu:
public class MainMenu {
int id;
String title;
String path;
List<SubMenu> sMain;
}
SubMenu:
public class SubMenu {
int id;
String title;
String path;
}
dao
userDao:
@Repository
public interface UserDao {
public User getUserByMessage(@Param("username") String username, @Param("password")String password);
public List<User> findAllUser(@Param("username")String username,@Param("pageStart")int pageState,@Param("pageSize")int pageSize);
public int getUserCount(@Param("username")String username);
public int updateState(Integer id,Boolean state);
public int addUser(User user);
public int delUser(int id);
public User findUser(int id);
public int updateUser(User user);
}
由于有一对多的查询,所以再加个MenuDao
@Repository
public interface MenuDao {
List<MainMenu> findAllMenu();
}
Mapper文件
UserMapper.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="com.zut.sportplay.dao.UserDao"> <select id="getUserByMessage" resultType="com.zut.sportplay.bean.User"> SELECT * FROM easyuser where username=#{username} and password = #{password} and state = 1 </select> <select id="findAllUser" resultType="com.zut.sportplay.bean.User"> SELECT * FROM easyUser <if test="username !=null "> WHERE username like #{username} </if> LIMIT #{pageStart},#{pageSize} </select> <select id="getUserCount" resultType="java.lang.Integer"> SELECT count(*) FROM `easyuser` <if test="username !=null "> WHERE username like #{username} </if> </select> <update id="updateState"> UPDATE easyuser SET state = #{state} WHERE id = #{id} </update> <insert id="addUser" > INSERT INTO easyuser (username,password,email,role,state) VALUE (#{username},#{password},#{email},#{role},#{state}) </insert> <delete id="delUser"> delete from easyuser where id = #{id} </delete> <select id="findUser" resultType="com.zut.sportplay.bean.User"> SELECT * FROM easyuser WHERE id = #{id} </select> <update id="updateUser"> UPDATE easyuser SET username = #{username} , password = #{password} , email = #{email} WHERE id = #{id} </update> </mapper>
Menu.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="com.zut.sportplay.dao.MenuDao"> <!--这个是一对多的查询内容 --> <resultMap id="menuMap" type="com.zut.sportplay.bean.MainMenu"> <id column="id" property="id"></id> <result column="title" property="title"></result> <result column="path" property="path"></result> <!--下面这个是对应的查询list集合 --> <collection property="sMain" ofType="com.zut.sportplay.bean.SubMenu"> <id column="sid" property="id"></id> <result column="stitle" property="title"></result> <result column="spath" property="path"></result> </collection> </resultMap> <select id="findAllMenu" resultMap="menuMap"> SELECT mm.*,sm.id as sid ,sm.title as stitle,sm.path as spath FROM mainmenu mm ,submenu sm WHERE mm.id = sm.mid; </select> </mapper>
UserController:里面对应dao层url的crud
@RestController public class UserController { @Autowired UserDao userDao; @RequestMapping("/alluser") public String getUserList(QueryInfo queryInfo){ //获取最大列编号和当前编号 int numbers = userDao.getUserCount("%" + queryInfo.getQuery() + "%"); int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize(); List<User> users = userDao.findAllUser("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize()); HashMap<String, Object> res = new HashMap<>(); res.put("numbers",numbers); res.put("data",users); String s = JSON.toJSONString(res); return s; } @RequestMapping("/userState") public String updateState(@RequestParam("id")Integer id,@RequestParam("state")Boolean state){ int i = userDao.updateState(id, state); return i>0?"success":"error"; } @RequestMapping("/addUser") public String addUser(@RequestBody User user){ user.setRole("普通用户"); user.setState(false); int i = userDao.addUser(user); return i>0?"success":"error"; } @RequestMapping("/deleteUser") public String deleteUser(int id){ int i = userDao.delUser(id); return i>0?"success":"error"; } @RequestMapping("/getupdate") public String getUpdateUser(int id){ User user = userDao.findUser(id); String u = JSON.toJSONString(user); return u; } @RequestMapping("/edituser") public String editUser(@RequestBody User user){ int i = userDao.updateUser(user); return i>0?"success":"error"; }
LoginController:
@RestController //@CrossOrigin("http://localhost:8080") public class LoginController { @Autowired public UserDao userDao; @RequestMapping("/login") public String login(@RequestBody User user){ String flag = "error"; User us = userDao.getUserByMessage(user.getUsername(), user.getPassword()); System.out.println("user:"+us); HashMap<String,Object> result = new HashMap<>(); if(us!=null){ flag = "ok"; } result.put("flag",flag); result.put("user",us); String json = JSON.toJSONString(result); return json; }
MenuController:
@RestController public class MenuController { @Autowired MenuDao menuDao; @RequestMapping("/menus") public String findAllMenus(){ HashMap<String,Object> data = new HashMap<>(); List<MainMenu> msins = menuDao.findAllMenu(); if(msins != null){ data.put("menus",msins); data.put("flag",200); }else { data.put("flag",404); } System.out.println(data); String s = JSON.toJSONString(data); return s; }
附录:前后端交互的时候会产生跨域的问题,要配置跨域的文件,具体信息如下所示:
@Configuration
public class WebConfig implements WebMvcConfigurer {
// 这是要解决跨域问题(全局配置类)
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
.allowCredentials(true)
.maxAge(3600);
}
}
最最重要的文件,项目启动类:
@SpringBootApplication
@MapperScan("com.zut.sportplay.dao")
public class SportplayApplication {
public static void main(String[] args) {
SpringApplication.run(SportplayApplication.class, args);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。