博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第二章 springboot+mybatis
阅读量:4490 次
发布时间:2019-06-08

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

一、手工创建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
3
org.springframework.boot
4
spring-boot-starter-jdbc
5
6
7
8
com.alibaba
9
druid
10
1.0.14
11
12
13
14
mysql
15
mysql-connector-java
16
runtime
17
18
19
20
org.mybatis
21
mybatis
22
3.2.8
23
24
25
org.mybatis
26
mybatis-spring
27
1.2.2
28
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.Driver2 jdbc.url=jdbc:mysql://127.0.0.1:3306/weibo3 jdbc.username=root4 jdbc.password=123456
View Code
      • application.properties文件是spring-boot的默认文件,一般各种配置(包括:数据源配置等)都配在这里
      • spring-boot的读取属性文件的方式也相当容易,读取属性文件常用的三种方式
        • 使用FileUtil去读:
        • 使用注解实现:
        • 使用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 @Configuration17 @MapperScan(basePackages = "com.weibo2.mapper")18 public class MyBatisConfig {19     @Autowired20     private Environment env;21     22     @Bean23     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     @Bean33     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 
2 3
4
5
6
7
8
9
10
11
12
13 id, content, owner, createtime, lastmodifytime, readcount 14
15
21
22 delete from t_weibo 23 where id = #{id,jdbcType=INTEGER} 24
25
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
33
34 insert into t_weibo 35
36
37 id, 38
39
40 content, 41
42
43 owner, 44
45
46 createtime, 47
48
49 lastmodifytime, 50
51
52 readcount, 53
54
55
56
57 #{id,jdbcType=INTEGER}, 58
59
60 #{content,jdbcType=VARCHAR}, 61
62
63 #{owner,jdbcType=VARCHAR}, 64
65
66 #{createtime,jdbcType=TIMESTAMP}, 67
68
69 #{lastmodifytime,jdbcType=TIMESTAMP}, 70
71
72 #{readcount,jdbcType=INTEGER}, 73
74
75
76
77 update t_weibo 78
79
80 content = #{content,jdbcType=VARCHAR}, 81
82
83 owner = #{owner,jdbcType=VARCHAR}, 84
85
86 createtime = #{createtime,jdbcType=TIMESTAMP}, 87
88
89 lastmodifytime = #{lastmodifytime,jdbcType=TIMESTAMP}, 90
91
92 readcount = #{readcount,jdbcType=INTEGER}, 93
94
95 where id = #{id,jdbcType=INTEGER} 96
97
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
106
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 @Repository10 public class WeiboDao {11     @Autowired12     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 @Service10 public class WeiboService {11     @Autowired12     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 @RestController15 @RequestMapping("/weibo")16 public class WeiboController {17     @Autowired18     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

你可能感兴趣的文章
软件工程结对作业
查看>>
Keil 4.0 生成bin文件
查看>>
sql语句的进化--hibernate篇
查看>>
python爬虫之cookie
查看>>
2017年5月29号课堂笔记
查看>>
HDU4247【瞎搞】
查看>>
lightoj 1125【背包·从n个选m个】
查看>>
HDU 1243 反恐训练营(最长公共序列)
查看>>
mysql数据库隔离级别
查看>>
(六)buildroot使用详解
查看>>
chrome修改UserAgent,调试
查看>>
Source Insight4.0 试用。。试用。。试用。。
查看>>
python循环for,range,xrange;while
查看>>
解决Ueditor在bootstarp 模态框中全屏问题
查看>>
POJ 1006 Biorhythms
查看>>
dubbo+zookeeper注册服务报错问题:No service registed on zookeeper
查看>>
极验滑动验证登录
查看>>
求多个数的质因子
查看>>
laravel的orm作用
查看>>
jQuery插件学习笔记
查看>>