博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java版电子商务spring cloud分布式微服务b2b2c社交电商 (六)springboot整合mybatis
阅读量:7021 次
发布时间:2019-06-28

本文共 2701 字,大约阅读时间需要 9 分钟。

电子商务社交平台源码请加企鹅求求:三五三六二四七二五九

引入依赖

在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/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver复制代码

这样,springboot就可以访问数据了。

创建数据库表

建表语句:

-- create table `account`# DROP TABLE `account` IF EXISTSCREATE 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层

@Mapperpublic 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")    List
findAccountList();}复制代码

service层

@Servicepublic 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 List
findAccountList() { return accountMapper.findAccountList(); }}复制代码

电子商务社交平台源码请加企鹅求求:三五三六二四七二五九

转载于:https://juejin.im/post/5cf8e4f55188253cec30632c

你可能感兴趣的文章
JAVA学习日记2——foreach的常用用法
查看>>
OpenSSL 创建私有CA及客户端请求证书
查看>>
Linux 之 shell 比较运算符
查看>>
window 7 下 将asp.net core程序部署在docker 上运行
查看>>
Linux下的高级文件权限ACL
查看>>
Linux下semop等待信号时出现Interrupted System Call错误(EINTR)
查看>>
SpringCloud(第 031 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务
查看>>
一道 Python 类的笔试题详解
查看>>
sysctl优化linux网络
查看>>
如何压缩 Outlook PST 和 OST 文件
查看>>
UIScrollView中的UITableView接收不到点击事件
查看>>
(12)Struts2中的Action
查看>>
基于jeasyframe框架进行开发项目实例
查看>>
WebRobot v1.5.3
查看>>
jquery构造函数分析
查看>>
手机输入法初始界面整理
查看>>
c/c++ 中的文件路径表示
查看>>
搭建Web服务器之Step8:CentOS6.3安装MySQL5.5
查看>>
关于github的吉祥物--章鱼猫
查看>>
高可用集群脑裂问题
查看>>