一、手工创建maven项目,目录结构如springboot+maven

注意:此时的WeiboApplication.java文件变更为:

 1 package com.weibo2;
 2
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5
 6 @SpringBootApplication
 7 public class WeiboApplication {
 8
 9     public static void main(String[] args) {
10         SpringApplication.run(WeiboApplication.class, args);
11     }
12 }

上述几点说明:

1、@SpringBootApplication等价于一下三个注解:

  • @Configuration:该注解指明该类由spring容器管理
  • @EnableAutoConfiguration:该注解是无xml配置启动的关键部分
  • @ComponentScan:该注解指定扫描包(如果主类不是位于根路径下,这里需要指定扫描路径),类似于spring的包扫描注解

2、主类是springboot程序的入口,最好位于根包路径下(例如,com.weibo2),这是推荐做法,方便扫描

引入eclipse后的项目结构为:

二、pom.xml在springboot+maven的基础上加入mybatis的相关依赖

 1 <!-- 与数据库操作相关的依赖 -->
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-jdbc</artifactId>
 5         </dependency>
 6         <!-- 使用数据源 -->
 7         <dependency>
 8             <groupId>com.alibaba</groupId>
 9             <artifactId>druid</artifactId>
10             <version>1.0.14</version>
11         </dependency>
12         <!-- mysql -->
13         <dependency>
14             <groupId>mysql</groupId>
15             <artifactId>mysql-connector-java</artifactId>
16             <scope>runtime</scope>
17         </dependency>
18         <!-- mybatis -->
19         <dependency>
20             <groupId>org.mybatis</groupId>
21             <artifactId>mybatis</artifactId>
22             <version>3.2.8</version>
23         </dependency>
24         <dependency>
25             <groupId>org.mybatis</groupId>
26             <artifactId>mybatis-spring</artifactId>
27             <version>1.2.2</version>
28         </dependency>

View Code

  • spring-boot-starter-jdbc:引入与数据库操作相关的依赖

  • druid:阿里巴巴的数据源
  • mysql-connector-java:mysql连接java的必须包,scope为runtime
  • mybatis + mybatis-spring:mybatis相关jar

三、属性文件:application.properties

1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://127.0.0.1:3306/weibo
3 jdbc.username=root
4 jdbc.password=123456

View Code

  • application.properties文件是spring-boot的默认文件,一般各种配置(包括:数据源配置等)都配在这里
  • spring-boot的读取属性文件的方式也相当容易,读取属性文件常用的三种方式
    • 使用FileUtil去读:第一章 属性文件操作工具类
    • 使用注解实现:第二章 第二个spring-boot程序
    • 使用Environment这个类来获取就行(直接使用Environment类的对象.getProperty()注意这个类的全类名是org.springframework.core.env.Environment)

四、mybatis配置文件:MybatisConfig.java

 1 package com.weibo2.config;
 2
 3 import javax.sql.DataSource;
 4
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.mybatis.spring.SqlSessionFactoryBean;
 7 import org.mybatis.spring.annotation.MapperScan;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.core.env.Environment;
12 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
13
14 import com.alibaba.druid.pool.DruidDataSource;
15
16 @Configuration
17 @MapperScan(basePackages = "com.weibo2.mapper")
18 public class MyBatisConfig {
19     @Autowired
20     private Environment env;
21
22     @Bean
23     public DruidDataSource dataSource() {
24         DruidDataSource dataSource = new DruidDataSource();
25         dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
26         dataSource.setUrl(env.getProperty("jdbc.url"));
27         dataSource.setUsername(env.getProperty("jdbc.username"));
28         dataSource.setPassword(env.getProperty("jdbc.password"));
29         return dataSource;
30     }
31
32     @Bean
33     public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
34         SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
35         fb.setDataSource(dataSource);
36         fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
37         return fb.getObject();
38     }
39 }

View Code

说明:

  • 该配置文件中使用Environment类的对象.getProperty()读取属性文件的内容
  • 类上加了两个注解:
    • @Configuration:指明该类由spring容器管理,该注解类似于spring的配置文件
    • @MapperScan:指定扫描的mapper接口所在的包
  • @Bean:用在方法上,告诉spring容器,可以从下面的方法中拿到一个bean
  • 在该类中,使用阿里巴巴的druid数据源定义了数据源Bean,根据数据源生成SqlSessionFactory,数据源必须指定,否则无法启动项目
  • PathMatchingResourcePatternResolver,是一个通配符的Resource查找器

五、其他层

1、mybatis的generator自动生成的:

 1 package com.weibo2.model;
 2
 3 import java.util.Date;
 4
 5 public class Weibo {
 6     private Integer id;
 7
 8     private String content;
 9
10     private String owner;
11
12     private Date createtime;
13
14     private Date lastmodifytime;
15
16     private Integer readcount;
17
18     public Integer getId() {
19         return id;
20     }
21
22     public void setId(Integer id) {
23         this.id = id;
24     }
25
26     public String getContent() {
27         return content;
28     }
29
30     public void setContent(String content) {
31         this.content = content == null ? null : content.trim();
32     }
33
34     public String getOwner() {
35         return owner;
36     }
37
38     public void setOwner(String owner) {
39         this.owner = owner == null ? null : owner.trim();
40     }
41
42     public Date getCreatetime() {
43         return createtime;
44     }
45
46     public void setCreatetime(Date createtime) {
47         this.createtime = createtime;
48     }
49
50     public Date getLastmodifytime() {
51         return lastmodifytime;
52     }
53
54     public void setLastmodifytime(Date lastmodifytime) {
55         this.lastmodifytime = lastmodifytime;
56     }
57
58     public Integer getReadcount() {
59         return readcount;
60     }
61
62     public void setReadcount(Integer readcount) {
63         this.readcount = readcount;
64     }
65 }

View Code

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 <mapper namespace="com.weibo2.mapper.WeiboMapper" >
  4   <resultMap id="BaseResultMap" type="com.weibo2.model.Weibo" >
  5     <id column="id" property="id" jdbcType="INTEGER" />
  6     <result column="content" property="content" jdbcType="VARCHAR" />
  7     <result column="owner" property="owner" jdbcType="VARCHAR" />
  8     <result column="createtime" property="createtime" jdbcType="TIMESTAMP" />
  9     <result column="lastmodifytime" property="lastmodifytime" jdbcType="TIMESTAMP" />
 10     <result column="readcount" property="readcount" jdbcType="INTEGER" />
 11   </resultMap>
 12   <sql id="Base_Column_List" >
 13     id, content, owner, createtime, lastmodifytime, readcount
 14   </sql>
 15   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
 16     select
 17     <include refid="Base_Column_List" />
 18     from t_weibo
 19     where id = #{id,jdbcType=INTEGER}
 20   </select>
 21   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
 22     delete from t_weibo
 23     where id = #{id,jdbcType=INTEGER}
 24   </delete>
 25   <insert id="insert" parameterType="com.weibo2.model.Weibo" >
 26     insert into t_weibo (id, content, owner,
 27       createtime, lastmodifytime, readcount
 28       )
 29     values (#{id,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, #{owner,jdbcType=VARCHAR},
 30       #{createtime,jdbcType=TIMESTAMP}, #{lastmodifytime,jdbcType=TIMESTAMP}, #{readcount,jdbcType=INTEGER}
 31       )
 32   </insert>
 33   <insert id="insertSelective" parameterType="com.weibo2.model.Weibo" >
 34     insert into t_weibo
 35     <trim prefix="(" suffix=")" suffixOverrides="," >
 36       <if test="id != null" >
 37         id,
 38       </if>
 39       <if test="content != null" >
 40         content,
 41       </if>
 42       <if test="owner != null" >
 43         owner,
 44       </if>
 45       <if test="createtime != null" >
 46         createtime,
 47       </if>
 48       <if test="lastmodifytime != null" >
 49         lastmodifytime,
 50       </if>
 51       <if test="readcount != null" >
 52         readcount,
 53       </if>
 54     </trim>
 55     <trim prefix="values (" suffix=")" suffixOverrides="," >
 56       <if test="id != null" >
 57         #{id,jdbcType=INTEGER},
 58       </if>
 59       <if test="content != null" >
 60         #{content,jdbcType=VARCHAR},
 61       </if>
 62       <if test="owner != null" >
 63         #{owner,jdbcType=VARCHAR},
 64       </if>
 65       <if test="createtime != null" >
 66         #{createtime,jdbcType=TIMESTAMP},
 67       </if>
 68       <if test="lastmodifytime != null" >
 69         #{lastmodifytime,jdbcType=TIMESTAMP},
 70       </if>
 71       <if test="readcount != null" >
 72         #{readcount,jdbcType=INTEGER},
 73       </if>
 74     </trim>
 75   </insert>
 76   <update id="updateByPrimaryKeySelective" parameterType="com.weibo2.model.Weibo" >
 77     update t_weibo
 78     <set >
 79       <if test="content != null" >
 80         content = #{content,jdbcType=VARCHAR},
 81       </if>
 82       <if test="owner != null" >
 83         owner = #{owner,jdbcType=VARCHAR},
 84       </if>
 85       <if test="createtime != null" >
 86         createtime = #{createtime,jdbcType=TIMESTAMP},
 87       </if>
 88       <if test="lastmodifytime != null" >
 89         lastmodifytime = #{lastmodifytime,jdbcType=TIMESTAMP},
 90       </if>
 91       <if test="readcount != null" >
 92         readcount = #{readcount,jdbcType=INTEGER},
 93       </if>
 94     </set>
 95     where id = #{id,jdbcType=INTEGER}
 96   </update>
 97   <update id="updateByPrimaryKey" parameterType="com.weibo2.model.Weibo" >
 98     update t_weibo
 99     set content = #{content,jdbcType=VARCHAR},
100       owner = #{owner,jdbcType=VARCHAR},
101       createtime = #{createtime,jdbcType=TIMESTAMP},
102       lastmodifytime = #{lastmodifytime,jdbcType=TIMESTAMP},
103       readcount = #{readcount,jdbcType=INTEGER}
104     where id = #{id,jdbcType=INTEGER}
105   </update>
106 </mapper>

View Code

 1 package com.weibo2.mapper;
 2
 3 import com.weibo2.model.Weibo;
 4
 5 public interface WeiboMapper {
 6     int deleteByPrimaryKey(Integer id);
 7
 8     int insert(Weibo record);
 9
10     int insertSelective(Weibo record);
11
12     Weibo selectByPrimaryKey(Integer id);
13
14     int updateByPrimaryKeySelective(Weibo record);
15
16     int updateByPrimaryKey(Weibo record);
17 }

View Code

2、WeiboDao.java

 1 package com.weibo2.dao;
 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Repository;
 5
 6 import com.weibo2.mapper.WeiboMapper;
 7 import com.weibo2.model.Weibo;
 8
 9 @Repository
10 public class WeiboDao {
11     @Autowired
12     private WeiboMapper weiboMapper;
13
14     public boolean add(Weibo weibo) {
15         return weiboMapper.insert(weibo) == 1;
16     }
17
18     public Weibo select(Integer id) {
19         return weiboMapper.selectByPrimaryKey(id);
20     }
21
22     public boolean updateSelective(Weibo record) {
23         return weiboMapper.updateByPrimaryKeySelective(record) == 1;
24     }
25
26     public boolean deleteById(Integer id) {
27         return weiboMapper.deleteByPrimaryKey(id) == 1;
28     }
29 }

View Code

3、WeiboService.java

 1 package com.weibo2.service;
 2
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5
 6 import com.weibo2.dao.WeiboDao;
 7 import com.weibo2.model.Weibo;
 8
 9 @Service
10 public class WeiboService {
11     @Autowired
12     private WeiboDao weiboDao;
13
14     public boolean addWeibo(Weibo weibo) {
15         return weiboDao.add(weibo);
16     }
17
18     public Weibo getWeibo(Integer id) {
19         return weiboDao.select(id);
20     }
21
22     public boolean updateWeibo(Weibo weibo) {
23         return weiboDao.updateSelective(weibo);
24     }
25
26     public boolean deleteWeibo(Integer id) {
27         return weiboDao.deleteById(id);
28     }
29
30 }

View Code

4、WeiboController.java

 1 package com.weibo2.controller;
 2
 3 import java.util.Date;
 4
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.RestController;
10
11 import com.weibo2.model.Weibo;
12 import com.weibo2.service.WeiboService;
13
14 @RestController
15 @RequestMapping("/weibo")
16 public class WeiboController {
17     @Autowired
18     private WeiboService weiboService;
19
20     @RequestMapping(value = "/addWeibo", method = RequestMethod.POST)
21     public boolean addWeibo(@RequestParam("content") String content, @RequestParam("owner") String owner) {
22         Weibo weibo = new Weibo();
23         weibo.setContent(content);
24         weibo.setOwner(owner);
25         weibo.setCreatetime(new Date());
26         weibo.setLastmodifytime(new Date());
27         weibo.setReadcount(0);
28         return weiboService.addWeibo(weibo);
29     }
30
31     @RequestMapping(value = "/getWeibo", method = RequestMethod.GET)
32     public Weibo getWeibo(@RequestParam("id") Integer id) {
33         return weiboService.getWeibo(id);
34     }
35
36     @RequestMapping(value = "/updateWeibo", method = RequestMethod.PUT)
37     public boolean updateWeibo(@RequestParam(value="id",required=true) Integer id,
38             @RequestParam(value="content",required=false) String content,
39             @RequestParam(value="owner",required=false) String owner) {
40         Weibo weibo = weiboService.getWeibo(id);
41         weibo.setContent(content);
42         weibo.setOwner(owner);
43         return weiboService.updateWeibo(weibo);
44     }
45
46     @RequestMapping(value = "/deleteWeibo", method = RequestMethod.DELETE)
47     public boolean deleteWeibo(@RequestParam("id") Integer id) {
48         return weiboService.deleteWeibo(id);
49     }
50 }

View Code

六、测试:

1、首先启动spring-boot:可以通过命令:mvn spring-boot:run

2、通过postman进行测试

转载于:https://www.cnblogs.com/wangna----558169/p/6187012.html

第二章 springboot+mybatis相关推荐

  1. 【高校宿舍管理系统】第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码

    第二章 整合Mybatis和写CRUD的基本流程以及使用代码生成器生成Mapper等相关代码 提示:本博客个为人独立博客,不是权威,仅供参考!所有思路只做交流之用!如有不足之处,望各位在评论区友善指正 ...

  2. 【微信开发第二章】SpringBoot实现微信公众号普通消息和模板消息回复

    前言 在进行微信公众号业务开发的时候,微信公众号的消息回复是非常重要的一环,而微信公众号消息回复分为:普通消息自动回复和模板消息回复.该篇文章会先使用微信测试工具过一遍流程,再使用代码进行实现,并且每 ...

  3. SpringBoot学习笔记-2:第二章 Spring Boot 配置

    第二章 Spring Boot 配置 1.YAML 配置 SpringBoot 全局配置文件 application.properties application.yml YAML 以数据为中心,比 ...

  4. 19年8月 字母哥 第一章 spring boot 2.x基础及概念入门 这里全部看完了 热部署没出来 第二章在前面2页 用热点公司网不行

    http://springboot.zimug.com/1233100   文档 http://www.zimug.com/page/5     字母哥个人博客 11111 第一章 spring bo ...

  5. springboot mybatis easyui 整合的一个小demo

    springboot mybatis easyui 整合的一个小demo 这是最终完成界面 话不多说 开整! 这是项目结构 数据库 表结构和数据库 (有点乱 之前本来是个正经图书表的 = =.) /* ...

  6. springboot mybatis 事务_SpringBoot 下 Mybatis 的缓存

    "IT魔幻屋"致力于让你遇见更好的自己! 说起 mybatis,作为 Java 程序员应该是无人不知,它是常用的数据库访问框架.与 Spring 和 Struts 组成了 Java ...

  7. SpringBoot+MyBatis搭建迷你小程序

    简介:用Spring Boot框架大大简化了新Spring应用的初始搭建以及开发过程,在开发人员中越来越受到欢迎.微信小程序作为目前炙手可热的应用,很有可能在未来占据轻应用的市场.本门课程的主要目的是 ...

  8. 探讨 | SpringBoot + MyBatis 多数据源事物问题

    这是小小本周的第二篇,本篇将会着重讲解关于SpringBoot + MyBatis 多数据源的事物的问题. 多数据源 此处模拟创建订单和扣减库存.先创建订单表和库存表 CREATE TABLE `t_ ...

  9. SpringBoot+MyBatis+Mysql 6.X 版本日期型数据获,时间错乱,jason序列化时间相差8小时问题...

    新项目是用的springboot+mybatis+mysql 6.0.6版本的驱动包来搭建的,在使用的过程中遇到以下2个问题 从mysql取的的数据日期时间,与真实的时间往后错乱了14个小时. spr ...

  10. springboot数据源oracle,springboot+mybatis中使用多数据源oracle数据库

    写在前面:工作原因,使用的是oracle数据库.由于之前没有接触过,写一篇博客做点笔记. 如果要在springboot项目中连接oracle,首先肯定是要先加入依赖,但是好像在maven仓库里没有or ...

最新文章

  1. 网页制作的中的一些工具代码
  2. Linux:几个重要的文件处理命令
  3. ST算法解决RMQ问题
  4. 重写0-1背包问题的回溯法,使算法能输出最优解
  5. 群策群力:破机房征求灵丹妙药
  6. [示例] 使用 TStopwatch 计时
  7. html自动适应布局,用纯CSS实现自适应布局表格
  8. python和java选择哪个-python与java,该选择哪一个?
  9. python列表所有元素平均值_【全网最简单Python教程】--10.列表元素的索引和返回索引值(Index函数使用)...
  10. §4.1.2数学归纳法证明不等式第6题 (复旦大学2004年保送生考试数学试题)
  11. linux mint 下如何制作win7启动盘
  12. 亚马逊关键词应该如何选择?
  13. TX-LCN分布式事务之TCC模式
  14. 一句话解释新西兰技术移民
  15. 网上百度的题目,随手写了一下
  16. 修改yum源带来的问题 curl: (35) Cannot communicate securely with peer: no common encryption algorithm(s).
  17. pm3包1.4版本发布----一个用于3组倾向性评分的R包
  18. 《数据库系统》(六)存储管理
  19. LTspice基础教程-008.LTspice PWL设置
  20. 计算机视觉入门 拜读

热门文章

  1. C++ 函数参数中和区别
  2. Hyperledger Fabric教程(8)--byfn.sh分析-script.sh
  3. Unity(八)脚本生命周期
  4. BigDecimal——大十进制-货币型-双精度-精确运算
  5. C语言真题考研pdf,中财信息学院C语言程序设计1999年考研真题.pdf
  6. apicloud开发时的一些注意点
  7. eclipse、EditPlus等编辑器选中列(块)的方法
  8. java中的装箱和拆箱
  9. RocketMQ Client 编码快速入门 与 可视化控制台
  10. IDEA配置更加流畅 写代码起飞停不下来~