如何使用SpringBoot连接我们的数据库,并且显示在网页上呢?
正文
新建或填写下列文件
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
.yml
server: port: 8081 spring.datasource: url: jdbc:mysql://112.126.72.101:3306/mysql?useUnicode=true&characterEncoding=utf8 username: root password: 123456789 mybatis: type-aliases-package: cn.jczb.service.pojo
UserMapper.java
public interface UserMapper extends Mapper<User> { }
User.java
@Table(name = "tb_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String userName; private String password; private String name; private Integer age; private Integer sex; private Date birthday; private Date created; private Date updated; public Long getId() { return id; } public void setId(Long 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 getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getUpdated() { return updated; } public void setUpdated(Date updated) { this.updated = updated; } @Override public String toString() { return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created + ", updated=" + updated + '}'; } }
UserService.java
@Service public class UserService { @Autowired private UserMapper userMapper; public User queryUserById(Long id){ return this.userMapper.selectByPrimaryKey(id); } }
UserController.java
@RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @GetMapping("{id}") public User queryUserById(@PathVariable("id")Long id){ return this.userService.queryUserById(id); } }
启动类
@SpringBootApplication @MapperScan("cn.jczb.service.jczbserviceprovider.mapper") public class JczbServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(JczbServiceProviderApplication.class, args); } }
建表
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for tb_user -- ---------------------------- DROP TABLE IF EXISTS `tb_user`; CREATE TABLE `tb_user` ( `id` int(11) NOT NULL, `user_name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `birthday` varchar(255) DEFAULT NULL, `created` varchar(255) DEFAULT NULL, `updated` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- -- Records of tb_user -- ---------------------------- INSERT INTO `tb_user` VALUES ('1', '啊', null, null, null, null, null, null, null);
启动 http://localhost:8081/user/1
这样就成功了!有兴趣就试试吧。
单元测试
测试方法
1.测试类上加@SpringBootTest注解
2.使用@Resource注入Service类
3.给测试方法上加@Test
4.直接调用需要测试的service的方法
其他代码
service
dao
mapper.xml
测试中出现的问题
1.org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
解决方案:mybatis-plus的依赖没有下载下来,导致mapper包扫描不到
2.java.lang.IllegalStateException: Failed to load ApplicationContext
解决方案:
启动类中加mapper扫描