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"> 2:application.properties4.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
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"; } }
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); }
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; } }
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); } }