springboot+mybatis+springmvc+mysql简单项目

1.新建一个springboot的maven项目:


springboot+mybatis+springmvc+mysql简单项目_第1张图片

2.数据库文件:


SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `sex` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO user VALUES ('1', '张三', '女');

INSERT INTO user VALUES ('2', '李四', '男');


3.引入依赖文件pom.xml:

      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0

  cn.springboot
  SpringBootFirst
  0.0.1-SNAPSHOT
  jar

  SpringBootFirst
  http://maven.apache.org

 
    UTF-8
    1.7
    1.2.0
    5.1.39
 


   
        org.springframework.boot  
        spring-boot-starter-parent  
        1.3.0.RELEASE  
  


   
         
         
            org.springframework.boot  
            spring-boot-starter-web  
       
 
         
            org.springframework.boot  
            spring-boot-starter-tomcat
            provided  
       

       
            org.springframework.boot
            spring-boot-starter-actuator
        

       
       
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        

         
         
            mysql  
            mysql-connector-java  
       
 
       
                com.google.code.gson
                gson
                2.7
            

          
         
         
            commons-lang  
            commons-lang  
            2.6  
       
 
        
       
            junit
            junit
        

  

    
     
     
        my-spring-boot
         
             
                org.springframework.boot  
                spring-boot-maven-plugin  
                 
                    true  
               
 
           
 
       
 
   
 

   

4.application.properties文件:

server.port = 8088
# jdbc:mysql
spring.datasource.url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=com.springboot.entity
mybatis.mapperLocations=classpath:mapper/*.xml


5.User实体类文件:

package com.springboot.entity;

public class User {

    private String id;
    private String name;
    private String sex;
    
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}


6.UserMapper文件:

package com.springboot.mapper;

import java.util.List;

import com.springboot.entity.User;

public interface UserMapper {

    /**获取所有用户信息
     * @return
     */
    public List getAllUsers();
}


7.json工具类:

package com.springboot.tools;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * json工具
 *
 * @author shenyan
 *
 */
public class JsonUtility {

    private static Logger logger = LogManager.getLogger(JsonUtility.class.getName());

    public static String convertBean2Json(Object bean) {
        ObjectMapper mapper = new ObjectMapper();
        String json = "";
        try {
            json = mapper.writeValueAsString(bean);
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage());
        }
        return json;
    }
}


8.UserController类:

package com.springboot.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.entity.User;
import com.springboot.mapper.UserMapper;
import com.springboot.tools.JsonUtility;

@Controller
public class UserController{

    @Autowired
    private UserMapper userMapper;
    
    @RequestMapping("/")
    @ResponseBody
    public String home() {
        return "Hello World!";
    }
   
    @RequestMapping("/getAllUsers")
    @ResponseBody
    public String getAllUsers() {
        List users = userMapper.getAllUsers();
        return JsonUtility.convertBean2Json(users);
    }
}


9.springboot启动类:

package com.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.springboot.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


10.UserMapper.xml文件:




    
    
   
        id, name, sex
   

    
   


      

你可能感兴趣的:(springboot+mybatis+springmvc+mysql简单项目)