SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)

来源:互联网 发布:windows副本不是正版 编辑:程序博客网 时间:2024/06/06 01:40

系列教程都是从网络上收集和本人的理解所编辑而成,仅供广大爱好者学习所用,请尊重本人的劳动成果。欢迎评论指正和转帖!(请保留连接谢谢!)



一、POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.fs</groupId><artifactId>springboot_mybatis_12_2</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>springboot_mybatis_12_2 Maven Webapp</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --><java.version>1.8</java.version></properties><dependencies><!-- 添加fastjson 依赖包. --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><!-- spring-boot-starter-web: MVC,AOP的依赖包.... --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!-- <version></version> 由于我们在上面指定了 parent(spring boot) --></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency><!-- 添加MySQL数据库驱动依赖包. --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- spring-boot mybatis依赖: 请不要使用1.0.0版本,因为还不支持拦截器插件, 1.1.1 是博主写帖子时候的版本,大家使用最新版本即可 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!--     MyBatis提供了拦截器接口,我们可以实现自己的拦截器,    将其作为一个plugin装入到SqlSessionFactory中。 Github上有位开发者写了一个分页插件,我觉得使用起来还可以,挺方便的。 Github项目地址: https://github.com/pagehelper/Mybatis-PageHelper     -->    <dependency>    <groupId>com.github.pagehelper</groupId>    <artifactId>pagehelper</artifactId>    <version>4.1.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><finalName>springboot_mybatis_12_2</finalName><plugins><!-- 这是spring boot devtool plugin (推荐) --><!--fork : 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins></build></project>


二、application.properties

###########################################################datasource -- \u6307\u5b9amysql\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f.########################################################spring.datasource.url = jdbc:mysql://localhost:3306/springboot_testspring.datasource.username = rootspring.datasource.password = fengsispring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10



三、App.java

package com.fs;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan("com.fs")public class App {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }}


四、Cat.java

package com.fs;public class Cat {    private int    id;    private String cat_name;    private int    cat_age;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getCat_name() {        return cat_name;    }    public void setCat_name(String cat_name) {        this.cat_name = cat_name;    }    public int getCat_age() {        return cat_age;    }    public void setCat_age(int cat_age) {        this.cat_age = cat_age;    }}


五、CatController.java

package com.fs;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class CatController {    @Autowired    private CatService catService;    @RequestMapping("/save")    public Cat save(Cat cat) {        int rs = catService.save(cat);        System.out.println("=======ok======" + rs);        return cat;    }    @RequestMapping("/update")    public int update(Cat cat) {        int rs = catService.update(cat);        return rs;    }    @RequestMapping("/delete")    public int delete(int id) {        int rs = catService.delete(id);        return rs;    }    @RequestMapping("/select")    public List<Cat> selectAll() {        List<Cat> list = catService.selectAll();        return list;    }}


六、CatService.java

package com.fs;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class CatService {    @Autowired    private CatMapper catMapper;    @Transactional // 添加事务.    public int save(Cat cat) {        return catMapper.save(cat);    }    public int update(Cat cat) {        return catMapper.update(cat);    }    public int delete(int id) {        return catMapper.delete(id);    }    public List<Cat> selectAll() {        return catMapper.selectAll();    }}


七、CatMapper.java

package com.fs;import java.util.List;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;@Mapperpublic interface CatMapper {    @Insert("insert into cat(cat_name,cat_age) values (#{cat_name},#{cat_age})")    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") // id自动增长    public int save(Cat cat);    @Update("update cat set cat_name = #{cat_name} where id = #{id}")    public int update(Cat cat);    @Delete("delete from cat where id = #{id}")    public int delete(int id);    @Select("select * from cat")    public List<Cat> selectAll();}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 劲的读音jin和jing的区别 坚劲的读音 劲怎么读音 劲霸夹克衫 劲霸男装休闲裤价格 劲霸男装裤子价格 劲霸男装休闲裤 男装裤 劲龙鲤鱼竿 金泉劲龙鲤鱼竿价格表 维赫劳施 劳丽诗 劳伦士 劳伦士g级 劳伦士s70l售价多少 劳伦诗 阿拉伯的劳伦斯原型 劳伦斯随笔 圣劳伦斯大学 瑞士劳伦斯净水器 圣劳伦斯散热器 詹尼斯劳伦斯 詹妮弗-劳伦斯 詹妮弗 劳伦斯 劳伦斯詹妮弗 詹妮佛劳伦斯 劳伦斯作品 劳伦斯布洛克 劳伦斯汽车 劳伦斯净水 劳伦斯五金 劳伦斯瓷砖 劳伦斯技术大学 劳伦斯探鱼器 劳伦斯净水器滤芯 劳伦斯许 dh劳伦斯 玛丽卡劳伦斯 劳伦斯泰勒 劳伦斯弟兄 劳伦斯奖