一、手工创建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的相关依赖
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
-
spring-boot-starter-jdbc:引入与数据库操作相关的依赖
- druid:阿里巴巴的数据源
- mysql-connector-java:mysql连接java的必须包,scope为runtime
- mybatis + mybatis-spring:mybatis相关jar
三、属性文件:application.properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
- application.properties文件是spring-boot的默认文件,一般各种配置(包括:数据源配置等)都配在这里
- spring-boot的读取属性文件的方式也相当容易,读取属性文件常用的三种方式
- 使用FileUtil去读:第一章 属性文件操作工具类
- 使用注解实现:第二章 第二个spring-boot程序
- 使用Environment这个类来获取就行(直接使用Environment类的对象.getProperty()注意这个类的全类名是org.springframework.core.env.Environment)
四、mybatis配置文件:MybatisConfig.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
说明:
- 该配置文件中使用Environment类的对象.getProperty()读取属性文件的内容
- 类上加了两个注解:
- @Configuration:指明该类由spring容器管理,该注解类似于spring的配置文件
- @MapperScan:指定扫描的mapper接口所在的包
- @Bean:用在方法上,告诉spring容器,可以从下面的方法中拿到一个bean
- 在该类中,使用阿里巴巴的druid数据源定义了数据源Bean,根据数据源生成SqlSessionFactory,数据源必须指定,否则无法启动项目
- PathMatchingResourcePatternResolver,是一个通配符的Resource查找器
五、其他层
1、mybatis的generator自动生成的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
3、WeiboService.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
4、WeiboController.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
六、测试:
1、首先启动spring-boot:可以通过命令:mvn spring-boot:run
2、通过postman进行测试