springboot企业级开发_springboot项目搭建

springboot企业级开发_springboot项目搭建企业级 SpringBoot 教程 (六)springboot整合mybatis

大家好,又见面了,我是你们的朋友全栈君。

引入依赖

在pom文件引入mybatis-spring-boot-starter的依赖:

<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter<artifactId>
     <version>1.3.0</version>
 </dependency>

引入数据库连接依赖:

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

引入数据源

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")
    List<Account> findAccountList();
}

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 List<Account> findAccountList() {
        return accountMapper.findAccountList();
    }
}

Spring Cloud大型企业分布式微服务云架构源码请加企鹅求求:一七九一七四三三八零

转载于:https://www.cnblogs.com/sunnysunny/p/10474849.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106951.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm 激活码2022[免费获取]

    (pycharm 激活码2022)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWNlbnNlSW…

    2022年3月21日
    130
  • 微信小程序和php交互_php接收json数据

    微信小程序和php交互_php接收json数据微信js源码Page({onLoad:function(){varthat=thiswx.request({//要交互页面的地址url:’http://localhost/php/index.php/Wxwater/Test/test’,data:{pid:1//data里边使我们要传递给PH…

    2022年9月17日
    1
  • C#中string.format用法详解「建议收藏」

    C#中string.format用法详解「建议收藏」string.Format对C#字符串格式化String.Format方法的几种定义:String.Format(String,Object)将指定的String中的格式项替换为指定的

    2022年7月3日
    15
  • 【Android音视频开发】【034】WEBRTC之ICE服务器搭建

    【Android音视频开发】【034】WEBRTC之ICE服务器搭建前篇由于ICE服务器是作为一个中转服务器来使用的,因此它必须搭建在外网,我是搭建在阿里云服务器上面的搭建ICE服务器需要一定的Linux经验和排错能力,没接触过Linux的量力而为切换到root用户sudosuroot安装opensslapt-getinstallopenssllibssl-devmake安装libevent2wgethttps://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar

    2022年6月8日
    33
  • python alpha量化交易软件_2019AI量化交易教程视频 AI量化交易模型教程 alpha量化选股模型交易系统 CTA型量化策略教程…

    python alpha量化交易软件_2019AI量化交易教程视频 AI量化交易模型教程 alpha量化选股模型交易系统 CTA型量化策略教程…第一部分:量化交易基础第1章量化交易基础:成对交易与模型自动化1.1量化交易简介1.2大纲简介与课程设置1.3成对交易算法1.4【Python实战】基于成对交易算法的目标股票池选取和自动化交易1.5成对交易问题探讨与模型优化1.6【Python实战】案例算法优化之动态成对交易模型第二部分:Alpha策略篇第2章寻找市场中的alpha2.1利用技术面数据挖掘A股中具有超额收益的股票…

    2022年6月26日
    32
  • linux 下 route命令,linux route命令详细说明和使用「建议收藏」

    linux 下 route命令,linux route命令详细说明和使用「建议收藏」linux下的route命令,虽然在嵌入式开发中很少用到,但当涉及到简单静态路由开发时,还是有用的。此外,在配置linux网络时也有可能用到。本文旨在了解linuxroute命令及其使用。作用route命令用于查看和配置linux内核路由表,也就是用来查看和配置linux的静态路由表。描述route命令操作基于linux内核路由表,它的主要作用是创建一个静态路由让指定的一个主机或者一个网络通过…

    2022年7月18日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号