spring boot+Mybatis

1:pom.xml 文件的相关配置

            

xml version="1.0" encoding="UTF-8"?>
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    springboot-mybatis
    springboot-mybatis
    1.0.0
    war
    springBoot-mybatis
    Spring Boot project
    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.2.RELEASE
        
    
    
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            5.1.21
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


2:application.properties
      
spring.datasource.url=jdbc:mysql://localhost:3306/yun
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8080


3:controller层


package com.yunst.controller;

import com.yunst.dao.UserMapper;
import com.yunst.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2017/1/16.
 */
@RestController
@RequestMapping({"/home"})
public class UserController {
    @Autowired
    UserMapper userMapper;
    @RequestMapping(value = "/user")
    @ResponseBody
    public String user(){

        User user = userMapper.findUserByName("普通管理员");
        return user.getId()+"======"+user.getUsername()+"-----"+user.getAge()+"=="+user.getLoginname();

    }
    @Autowired
    UserMapper userMapper1;
    @RequestMapping(value = "/insertuser")
    @ResponseBody
    public String insertuser() {

         User user = userMapper1.insertUser("会员", 20, "猪八戒");
         return "user";
    }

    @Autowired
    UserMapper userMapper2;
    @RequestMapping(value = "/deleteuser")
    @ResponseBody
    public String deletetuser() {

        userMapper2.deleteUser(4);
        return "user";
    }

    @Autowired
    UserMapper userMapper3;
    @RequestMapping(value = "/updateuser")
    @ResponseBody
    public String updateuser() {

        User user = userMapper3.updateUser("会员","猪八戒",3);
        return "user";
    }
}





4:dao层
package com.yunst.dao;

import com.yunst.domain.User;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;

/**
 * Created by Administrator on 2017/1/16.
 */
@Mapper
public interface UserMapper {
    @Select("select * from tb_user where username = #{username}")
    User findUserByName(@Param("username")String username);

    @Insert("insert into tb_user(username,age,loginname) values (#{username},#{age},#{loginname})")
    User insertUser(@Param("username") String username,@Param("age") int age,@Param("loginname") String loginname);

    @Delete("delete from tb_user where id=#{id}")
    User deleteUser(@Param("id") int id);


    @Update("update tb_user set username=#{username},loginname=#{loginname} where id=#{id}")
    User updateUser(@Param("username") String username,@Param("loginname") String loginname,@Param("id") int id);


}



5:domain

package com.yunst.domain;

/**
 * Created by Administrator on 2017/1/16.
 */
public class User {
    private int id;
    private String username;
    private int age;
    private String loginname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }
}


6:Application


package com.yunst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Administrator on 2017/1/16.
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args){

        SpringApplication.run(Application.class,args);

    }

}



mysql数据库:

CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户的id',
  `username` varchar(32) NOT NULL COMMENT '用户的用户名',
  `age` int(11) DEFAULT '0' COMMENT '用户的年龄',
  `loginname` varchar(32) NOT NULL COMMENT '用户的登录名',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;



insert  into `tb_user`(`id`,`username`,`age`,`loginname`) values (1,'无敌级管理员',100,'admin'),(2,'普通管理员',100,'swkong'),(3,'会员',20,'猪八戒'),(4,'会员',20,'猪八戒');

你可能感兴趣的:(框架项目)