springboot系列教程(七)——整合mybatis
来源:互联网 发布:js use strict 编辑:程序博客网 时间:2024/06/06 00:32
sql
-- ------------------------------ Table structure for user-- ----------------------------DROP TABLE IF EXISTS `user`;CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `money` double NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;-- ------------------------------ Records of user-- ----------------------------INSERT INTO `user` VALUES ('1', '11', 'a');INSERT INTO `user` VALUES ('2', '22', 'b');
pom文件
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.0</version> </dependency>
配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456
model类
package com.example.demo.model;/** * @author :小虎 * @date :2017/12/22 */public class User { private int id ; private String name ; private double money; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; }}
dao类
package com.example.demo.dao;import com.example.demo.model.User;import org.apache.ibatis.annotations.*;import java.util.List;/** * @author :小虎 * @date :2017/12/25 */@Mapperpublic interface UserMapper { @Insert("insert into user(name, money) values(#{name}, #{money})") int add(@Param("name") String name, @Param("money") double money); @Update("update user set name = #{name}, money = #{money} where id = #{id}") int update(@Param("name") String name, @Param("money") double money, @Param("id") int id); @Delete("delete from user where id = #{id}") int delete(int id); @Select("select id, name as name, money as money from user where id = #{id}") User findAccount(@Param("id") int id); @Select("select id, name as name, money as money from user") List<User> findAccountList();}
service类
package com.example.demo.service;import com.example.demo.dao.UserMapper;import com.example.demo.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;/** * @author :小虎 * @date :2017/12/25 */@Servicepublic class UserService { @Autowired UserMapper userMapper; public List<User> findAccountList() { return userMapper.findAccountList(); }}
web类
package com.example.demo.web;import com.example.demo.model.User;import com.example.demo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * @author :小虎 * @date :2017/12/25 */@RestControllerpublic class UserController { @Autowired UserService userService; @RequestMapping("/") public List<User> list(){ return userService.findAccountList(); }}
源代码
https://github.com/itmybaby/springboot/tree/master/study7
阅读全文