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

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿前面没头发怎么办 幼儿头发稀黄怎么办 头发干燥不直怎么办 小孩头发变少了怎么办 掉头发掉眉毛怎么办 剪了学生头会翘怎么办 笑起来有眼袋怎么办 早上起来眼袋重怎么办 45岁眼袋很大怎么办 五十岁眼袋很大怎么办 心情不好压力大怎么办 头发稀少脱发严重怎么办 骨头不痛肌肉痛怎么办 两额头没有头发怎么办 头发软化棉了怎么办 发际线头发细软怎么办 头顶部头发稀少怎么办 刚割完爆皮龟头太敏感怎么办 头皮手术后洗头怎么办 加密狗掉了怎么办 头发发尾干枯怎么办 头发太细太容易掉怎么办 发际线掉头发怎么办 加密狗时间到怎么办 短发太难看了怎么办 整块指甲掉了怎么办 超短发太难看怎么办 破解不了的wifi怎么办 烫头发头皮受损怎么办 搓澡后四肢瘙痒怎么办 没洗头头发痒怎么办 头发痒痒的厉害怎么办 洗冷水头头疼怎么办 孕妇用冷水洗头怎么办 孕妇洗了冷水澡怎么办 运动后头发出汗怎么办 洗冷水头头痛怎么办 洗头掉发厉害怎么办 青少年掉头发怎么办呢 洗头频繁掉头发怎么办 怀孕用了吹风机怎么办