SpringBoot整合myBatis操作mySQL数据库——第二节

在后台开发中,我们需要平凡的操作数据库,现在用mybatis来操作数据库。这里写一个查询数据库

1.在pom.xml配置文件中添加myBatis的依赖

         <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- alibabajson转换器 -->
            <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.40</version>
        </dependency>

2.在application.properties配置文件中 添加数据相关的配置

mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver      //连接数据库的驱动
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8   //连接数据库的地址
spring.datasource.username = root
spring.datasource.password = root

3.在mySql数据库建mybatis库(和连接地址后面端口的库名称要一样)建表,如图

4.建立表中对应的实体类

public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String userName;
    private String password;
    private String authority;
    private String phone;
    
    
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAuthority() {
        return authority;
    }
    public void setAuthority(String authority) {
        this.authority = authority;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    
}

5.在写一个UserMapper接口

@Mapper      //注意在次接口类定义方法时候,不能定义相同的方法名,会生成相同的id。也就是说不能重载
public interface UserMapper {
    @Select("select * from user")
    List<User> findAll();
}

@Mapper :添加此注解后在,这个接口在编译后生成相应的实现类

6.在写一个UserControl 类

@RestController  // 相当于@ResponseBody + @Controller合在一起的作用
@EnableTransactionManagement    //开启事务
public class UserControl {
    @Autowired  
    UserMapper userMapper;

        
        @RequestMapping("/findAll")
        public JSONObject findAll(){
            List<User> userList = userMapper.findAll();
            JSONObject json =new JSONObject();
            json.put("data",userList);
            return json;
        }
    }

    @Autowired  :
它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法

7.运行项目,在浏览器中输入:htttp://localhost:8080/findAll 就会出现如图界面

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值