zoukankan      html  css  js  c++  java
  • 使用idea+springboot+Mybatis搭建web项目

    使用idea+springboot+Mybatis搭建web项目

    springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便。

    1、创建项目project,然后选择Spring initializr,点击下一步 
    这里写图片描述

    2、按图示进行勾选,点击下一步,给项目起个名字,点击确定。

    这里写图片描述

    3、项目生成有,点击add as maven project,idea 会自动下载jar包,时间比较长 
    这里写图片描述

    4、项目生成后格式如下图所示: 
    这里写图片描述 
    其中DemoApplication.java是项目主入口,通过run/debug configuration进行配置,就可运行,因为集成了tomcat,所以该项目只需启动一遍即可,不用每次修改代码后重启项目,但是修改代码后需重新编译下,新代码才会生效。 
    这里写图片描述 
    5、生成的项目中,resources文件夹下,static文件夹下存放静态文件,比如css、js、html和图片等 
    templates下存放html文件,controller默认访问该文件夹下的html文件。 
    这个在application.properties配置文件中是可以修改的。 
    6、在application.properties中配置数据库信息

    spring.datasource.url = jdbc:mysql://localhost:3306/test
    spring.datasource.username = root
    spring.datasource.password =  1234
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    #页面热加载
    spring.thymeleaf.cache = false

    创建一个controller类:helloworld

    @Controller
    @EnableAutoConfiguration
    public class HelloWorld {
        @Autowired
        private IRegService regService;
        @RequestMapping("/")
        String home() {
            return "index";
        }
         @RequestMapping("/reg")
        @ResponseBody
        Boolean reg(@RequestParam("loginPwd") String loginNum, @RequestParam("userId") String userId ){
            String pwd = creatMD5(loginNum);
            System.out.println(userId+":"+loginNum);
            regService.regUser(userId,pwd);
            return true;
        }
        private String creatMD5(String loginNum){
            // 生成一个MD5加密计算摘要
            MessageDigest md = null;
            try {
                md = MessageDigest.getInstance("MD5");
                md.update(loginNum.getBytes());
    
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            return new BigInteger(1, md.digest()).toString(16);
        }
    }

    user类:

    public class User {
        private String id;
        private String userId;
        private String pwd;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }
    

    新建一个mapper接口

    public interface UserMapper {
        @Select("select * from users where userId = #{userId}")
        User findUserByUserid(@Param("userId") String userId);
        @Insert("insert into users (userId,pwd) values (#{userId},#{pwd})")
        boolean insertUsers (@Param("userId") String userId,@Param("pwd") String pwd);
    }

    service接口及实现:

    public interface IRegService {
        boolean regUser(String uerId,String pwd);
    }
    @Service()
    public class RegService implements IRegService {
        @Autowired
        private UserMapper userMapper;
        @Override
        public boolean regUser(String uerId, String pwd) {
    
            Boolean flag;
            try {
                flag = userMapper.insertUsers(uerId,pwd);
            }catch (Exception e){
                return false;
            }
            return flag;
        }
    }
    

    最后在主类名上添加mapperscan包扫描:

    @SpringBootApplication
    @MapperScan("com.example.mapper")
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    这是项目最后的包结构:

    这里写图片描述 
    注意点:1、DemoApplication类跟controller包等平行 
    2、@controller注解返回指定页面,本文中即返回index页面,也就是templates文件夹下的index.html 
    3、如果需要返回json字符串、xml等,需要在有@controller类下相关的方法上加上注解@responsebody 
    4、@restcontroller注解的功能等同于@controller和@responsebody 
    有问题请留言,一起讨论。 
    5、springboot默认缓存templates下的文件,如果html页面修改后,看不到修改的效果,设置spring.thymeleaf.cache = false即可

    转自:http://blog.csdn.net/alantuling_jt/article/details/54893383

  • 相关阅读:
    linux定时器【转】
    Linux Timer定时器【转】
    Linux使用一个定时器实现设置任意数量定时器功能【转】
    Ubuntu 12.04将默认集成Landscape管理套件【转】
    【Linux系统编程应用】Linux音频编程基础(一)【转】
    CRT/LCD/VGA Information and Timing【转】
    记录自己对EventLoop和性能问题处理的一点心得【转】
    Linux下select&poll&epoll的实现原理(一)【转】
    MQTT--入门【转】
    RESTful 个人理解总结【转】
  • 原文地址:https://www.cnblogs.com/zjoch/p/7615154.html
Copyright © 2011-2022 走看看