用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

hello你好    -  云代码空间

——

企业级 SpringBoot 教程 (六)springboot整合mybatis

2019-03-11|477阅||

摘要:企业级 SpringBoot 教程 (六)springboot整合mybatis

引入依赖

 

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

1
2
3
4
5
<dependency>
     <groupId>org.mybatis.spring.boot</groupId>
     <artifactId>mybatis-spring-boot-starter<artifactId>
     <version>1.3.0</version>
 </dependency>

  引入数据库连接依赖:

1
2
3
4
5
6
7
8
9
10
<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配置文件中引入数据源:

1
2
3
4
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就可以访问数据了。

创建数据库表

建表语句:

1
2
3
4
5
6
7
8
9
10
11
-- 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层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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大型企业分布式微服务云架构源码请加企鹅求求:yuncode.net
顶 0踩 0收藏
文章评论
    发表评论