标签:扫描 classpath print 配置数据源 l数据库 return bat time load
1.创建mysql数据库,并添加数据:
创建数据库访问层,实体类,业务层,控制层:
数据库访问层:
代码:
package com.xys.springnew.mapper; import com.xys.springnew.entity.User; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { User getById(int id); }
代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xys.springnew.mapper.UserMapper"> <select id="getById" resultType="com.xys.springnew.entity.User"> SELECT * FROM t_user where id = #{id} </select> </mapper>
实体类:
代码:
package com.xys.springnew.entity; public class User { private int id; private String name; private String password; 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 String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ‘}‘; } }
业务层接口:
代码:
package com.xys.springnew.service; import com.xys.springnew.entity.User; public interface IUserService { public User getById(int id); }
业务层实现类:
代码:
package com.xys.springnew.service.impl; import com.xys.springnew.entity.User; import com.xys.springnew.mapper.UserMapper; import com.xys.springnew.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class IUserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public User getById(int id) { return userMapper.getById(id); } }
控制层:
代码:
package com.xys.springnew.controller; import com.xys.springnew.entity.User; import com.xys.springnew.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("user") public class UserController { @Autowired private IUserService iUserService; @GetMapping("{id}") public User getById(@PathVariable("id") int id) { System.out.println("id" + id); System.out.println("查询操作"); return iUserService.getById(id); } }
配置mybatis:
代码:
#配置数据源信息 datasource: url: jdbc:mysql://localhost:3306/springboot_test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.jdbc.Driver # 扫描mapper;配置显示执行的sql mybatis: mapper-locations: classpath:/mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类中添加扫描mapper的注解:
访问url:
标签:扫描 classpath print 配置数据源 l数据库 return bat time load
原文地址:https://www.cnblogs.com/xuyinshan/p/14172076.html