Springboot整合mybtis(注解与xml)、jpa、reids

SpringBoot单元测试与Idea热部署、读取配置文件 

 SpringbootData中实现JPA

1、整合mybatis

2、整合jpa

3、整合redis

 
1、springboot整合mybatis(注解与配置文件)
1、引入pom文件
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.4</version>
</dependency>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>
2、创建实体
package city.albert.entity;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  11:32 AM
 */
public class Article {

    private int id;
    private String title;
    private String content;

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

 

3、编写mapper
package city.albert.mapper;

import city.albert.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  11:34 AM
 */
@Mapper
public interface ArticleMapper {


    Article queryById(Integer id);

    @Select("select * from t_article where id=#{xxx}")
    Article queryByAnnId(Integer id);
}

 

4、编写xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTDMapper3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="city.albert.mapper.ArticleMapper">
    <select id="queryById" resultType="city.albert.entity.Article">
        SELECT * from t_article where id=#{xxx}
    </select>

</mapper>

 

5、配置文件
#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

#mybtis配置
#开启驼峰
mybatis.configuration.map-underscore-to-camel-case=true
#指定mapper的xml文件
mybatis.mapper-locations=classpath*:mapper/*.xml
#指定实体别名
#mybatis.type-aliases-package=city.albert.entity

 

6、测试
@Autowired
private ArticleMapper articleMapper;

@Test
public void mybatis(){
    Article article = articleMapper.queryById(1);
    System.out.println(article);
    Article article1 = articleMapper.queryByAnnId(1);
    System.out.println(article1);

}

 

2、springboot整合JPA
1、引入pom文件
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <version>2.1.6.RELEASE</version>
</dependency>

 

2、编写实体类
package city.albert.entity;


import javax.persistence.*;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  11:26 AM
 */
@Entity
@Table(name = "t_comment")
public class JpaComment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @Column(name = "a_id")
    private int aId;
    private String author;
    private String content;

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", aId=" + aId +
                ", author='" + author + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getaId() {
        return aId;
    }

    public void setaId(int aId) {
        this.aId = aId;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

 

3、编写mapper
package city.albert.mapper;

import city.albert.entity.JpaComment;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  11:58 AM
 */
public interface  JpaCommentMapper extends JpaRepository<JpaComment,Integer> {
}

 

4、配置数据库连接,并测试
#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

 

测试

@Autowired
private JpaCommentMapper jpaCommentMapper;

@Test
public void jpa(){
    List<JpaComment> all = jpaCommentMapper.findAll();
    for (JpaComment jpaComment : all) {
        System.out.println(jpaComment);
    }

 

3、springboot整合reids
1、引入pom文件
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-redis</artifactId>
   <version>1.4.5.RELEASE</version>
</dependency>

 

2、创建实体对象
package city.albert.entity;

import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;

import javax.persistence.Id;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  12:36 PM
 * @RedisHash 指定在redis存储空间中的存储位置
 */
@RedisHash(value = "person")
public class Person {

    /**
     * @Id 指定主键 默认在redis中自动生成字符创的主键
     */
    @Id
    private String id;
    /**
     * @Indexed 指定在redis空间自动生成二级索引,如果没有设置二级索引通过该字段查询则为空
     */
    @Indexed
    private String name;
    @Indexed
    private Integer age;

    private Address address;

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

 

package city.albert.entity;

import org.springframework.data.redis.core.index.Indexed;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  12:36 PM
 */
public class Address {


    @Indexed
    private String city;
    @Indexed
    private String country;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", country='" + country + '\'' +
                '}';
    }
}

 

3、创建mapper
package city.albert.mapper;

import city.albert.entity.Person;
import org.springframework.data.repository.CrudRepository;

/**
 * @author niunafei
 * @function
 * @email niunafei0315@163.com
 * @date 2020/6/30  12:43 PM
 */
public interface PersonRepository extends CrudRepository<Person, String> {

    /**
     * 更具springdata的命名规则进行查询
     * @param name
     * @return
     */
    Person findByAddress_City(String name);
}

 

 

4、配置文件并测试
#Reids
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.password=

 

测试:

    @Autowired
    private PersonRepository repository;
    @Test
    public void redis(){
        Person person=new Person();
        person.setName("张三");
        person.setAge(12);
        Address address=new Address();
        address.setCity("北京");
        address.setCountry("中国");
        person.setAddress(address);

        repository.save(person);

        Person byAddress_city = repository.findByAddress_City("北京");
        System.out.println(byAddress_city);

    }

 

posted @ 2020-06-30 13:00  albert飞的博客  阅读(437)  评论(0编辑  收藏  举报