一、手工创建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 23 6 7org.springframework.boot 4spring-boot-starter-jdbc 58 12 13com.alibaba 9druid 101.0.14 1114 18 19mysql 15mysql-connector-java 16runtime 1720 24org.mybatis 21mybatis 223.2.8 2325 org.mybatis 26mybatis-spring 271.2.2 28
-
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
-
-
- 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 }
说明:
-
- 该配置文件中使用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 }
1 2 34 5 126 7 8 9 10 11 13 id, content, owner, createtime, lastmodifytime, readcount 14 15 2122 delete from t_weibo 23 where id = #{id,jdbcType=INTEGER} 24 2526 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 3334 insert into t_weibo 35 7636 5537 id, 38 3940 content, 41 4243 owner, 44 4546 createtime, 47 4849 lastmodifytime, 50 5152 readcount, 53 5456 7557 #{id,jdbcType=INTEGER}, 58 5960 #{content,jdbcType=VARCHAR}, 61 6263 #{owner,jdbcType=VARCHAR}, 64 6566 #{createtime,jdbcType=TIMESTAMP}, 67 6869 #{lastmodifytime,jdbcType=TIMESTAMP}, 70 7172 #{readcount,jdbcType=INTEGER}, 73 7477 update t_weibo 78 9779 95 where id = #{id,jdbcType=INTEGER} 9680 content = #{content,jdbcType=VARCHAR}, 81 8283 owner = #{owner,jdbcType=VARCHAR}, 84 8586 createtime = #{createtime,jdbcType=TIMESTAMP}, 87 8889 lastmodifytime = #{lastmodifytime,jdbcType=TIMESTAMP}, 90 9192 readcount = #{readcount,jdbcType=INTEGER}, 93 9498 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
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 }
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 }
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 }
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 }
六、测试:
1、首先启动spring-boot:可以通过命令:mvn spring-boot:run
2、通过postman进行测试