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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于height:100%和height:100vh的区别

    关于height:100%和height:100vh的区别关于height:100%和height:100vh的区别vh就是当前屏幕可见高度的1%,也就是说height:100vh==height:100%;但是当元素没有内容时候,设置height:100%,该元素不会被撑开,此时高度为0,但是设置height:100vh,该元素会被撑开屏幕高度一致。…

    2022年4月28日
    79
  • tomcat日志详解[通俗易懂]

    tomcat日志详解[通俗易懂]文章目录tomcat日志配置tomcat日志文件详解catalina.outcatalina.YYYY-MM-DD.loglocalhost.YYYY-MM-DD.loglocalhost_access_log.YYYY-MM-DD.txthost-manager.YYYY-MM-DD.logmanager.YYYY-MM-DD.log访问日志详细配置tomcat日志文件切割tomcat日志配…

    2022年6月20日
    41
  • JavaScript:三目运算符

    JavaScript:三目运算符HELLO大家好!三目运算符是一个非常简单且使用的运算符。是由两个运算符连接的三个操作数据或者表达式条件表达式?表达式1:表达式0当条件表达式为true则选择表达式1,反之false则选择表达式0举个栗子varage=15;console.log(age<18?’未成年’:’成年’);结果为:···本人写博客就是想记录一下自己所学的知识(目前正在学习中),巩固知识加深记忆,也顺便分享一下自己的所学,有什么地方写的不对,希望大家可以多多指出,让我及时改正。如果我分享的

    2022年6月17日
    33
  • stun client java实现_stun 协议客户端实现

    stun client java实现_stun 协议客户端实现/**Spider–AnopensourceClanguagetoolkit.**Copyright(C)2011,Inc.**lidp**Thisprogramisfreesoftware,distributedunderthetermsof*theGNUGeneralPublicLicenseVersion2.Seethe…

    2022年7月16日
    18
  • java高级语言程序设计_高级程序设计语言包括

    java高级语言程序设计_高级程序设计语言包括Java程序设计(高级及专题)- GUI

    2022年4月22日
    38
  • python deepcopy「建议收藏」

    python deepcopy「建议收藏」1.copy.copy浅拷贝只拷贝父对象,不会拷贝对象的内部的子对象。2.copy.deepcopy深拷贝拷贝对象及其子对象一个很好的例子:import copya = [1, 2, 3, 4, [‘a’, ‘b’]]  #原始对象b = …

    2022年10月2日
    2

发表回复

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

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