springboot整合mybatis并访问数据库的过程
这篇文章主要讲解了“springboot整合mybatis并访问数据库的过程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“springboot整合mybatis并访问数据库的过程”吧!
创新互联公司专业为企业提供太和网站建设、太和做网站、太和网站设计、太和网站制作等企业网站建设、网页设计与制作、太和企业网站模板建站服务,十余年太和做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
引入依赖
在pom文件引入mybatis-spring-boot-starter的依赖:
org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0
引入数据库连接依赖:
MySQL mysql-connector-java runtime com.alibaba druid 1.0.29
引入数据源
application.properties配置文件中引入数据源:
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
这样,springboot就可以访问数据了。
创建数据库表
建表语句:
-- create table `account` # DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `money` double DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `account` VALUES ('1', 'aaa', '1000'); INSERT INTO `account` VALUES ('2', 'bbb', '1000'); INSERT INTO `account` VALUES ('3', 'ccc', '1000');
具体实现
这篇文篇通过注解的形式实现。
创建实体:
public class Account { private int id ; private String name ; private double money; setter... getter... }
dao层
@Mapper public interface AccountMapper { @Insert("insert into account(name, money) values(#{name}, #{money})") int add(@Param("name") String name, @Param("money") double money); @Update("update account 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 account where id = #{id}") int delete(int id); @Select("select id, name as name, money as money from account where id = #{id}") Account findAccount(@Param("id") int id); @Select("select id, name as name, money as money from account") ListfindAccountList(); }
service层
@Service public class AccountService { @Autowired private AccountMapper accountMapper; public int add(String name, double money) { return accountMapper.add(name, money); } public int update(String name, double money, int id) { return accountMapper.update(name, money, id); } public int delete(int id) { return accountMapper.delete(id); } public Account findAccount(int id) { return accountMapper.findAccount(id); } public ListfindAccountList() { return accountMapper.findAccountList(); } }
controller层,构建restful API
package com.forezp.web; import com.forezp.entity.Account; import com.forezp.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by fangzhipeng on 2017/4/20. */ @RestController @RequestMapping("/account") public class AccountController { @Autowired AccountService accountService; @RequestMapping(value = "/list", method = RequestMethod.GET) public ListgetAccounts() { return accountService.findAccountList(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Account getAccountById(@PathVariable("id") int id) { return accountService.findAccount(id); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "money", required = true) double money) { int t= accountService.update(name,money,id); if(t==1) { return "success"; }else { return "fail"; } } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable(value = "id")int id) { int t= accountService.delete(id); if(t==1) { return "success"; }else { return "fail"; } } @RequestMapping(value = "", method = RequestMethod.POST) public String postAccount(@RequestParam(value = "name") String name, @RequestParam(value = "money") double money) { int t= accountService.add(name,money); if(t==1) { return "success"; }else { return "fail"; } } }
通过postman测试通过。
感谢各位的阅读,以上就是“springboot整合mybatis并访问数据库的过程”的内容了,经过本文的学习后,相信大家对springboot整合mybatis并访问数据库的过程这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
网页标题:springboot整合mybatis并访问数据库的过程
转载来源:http://lswzjz.com/article/jggiid.html