SpringBoot框架连接数据库实战(超详细)

SpringBoot框架用于实战(可直接下载源码进行拓展)



步骤:

  1. 创建数据库mr_wen,新建两条测试数据
  2. 创建SpringBoot项目
  3. 编写代码块,并进行单元测试

    创建数据库mr_wen,新建两条测试数据

  • create DATABASE mr_wen;
    use mr_wen;
    create table `product_category` (
        `category_id` int not null auto_increment,
        `category_name` varchar(64) not null ,
        `category_type` int not null  ,
        primary key (`category_id`)
    );

    创建SpringBoot项目(图解)

     创建项目

     创建完成


     编写代码块

  • 检查并导入依赖(pom.xml)
  • <dependencies>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
    
  • 测试项目是否可运行
  • 编写application.properties(用于连接数据库)
  • Spring.dataSource.driver-class-name=com.mysql.cj.jdbc.Driver
    Spring.dataSource.url=jdbc:mysql://localhost:3306/mr_wen?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false
    Spring.dataSource.username=root
    Spring.dataSource.password=root
    
    Spring.jpa:shop-sql:true
    
    
  • com.example.demo目录下创建包名dataobject,并创建类ProductCategory实体类
  • package com.example.demo.dataobject;
    
    import lombok.Data;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    //需要idea安装lombok插件,也可以自行添加G/S方法,toStirng方法
    @Entity
    @Data
    public class ProductCategory {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer categoryId;
    
    
        private String categoryName;
    
        private Integer categoryType;
    
    
    }
    
  •  com.example.demo目录下创建包名dao,并创建类ProductCategoryDao持久层
  • package com.example.demo.dao;
    
    import com.example.demo.dataobject.ProductCategory;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface ProductCategoryDao extends JpaRepository<ProductCategory,Integer> {
        List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
    }
  • 进行测试(写完程序进行单元测试可以随时检验代码的错误)
  • package com.example.demo.dao;
    
    import com.example.demo.dataobject.ProductCategory;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.Arrays;
    import java.util.List;
    
    import static org.junit.Assert.*;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ProductCategoryDaoTest {
    
        @Autowired
        private ProductCategoryDao productCategoryDao;
    
        /*查询*/
        @Test
        public void findOne(){
            ProductCategory productCategory=productCategoryDao.findById(1).get();
            Assert.assertNotNull(productCategory);
            System.out.println("productCategory = " + productCategory);
        }
    
        /*测试新增数据*/
        @Test
        public void saveTest(){
            ProductCategory productCategory=new ProductCategory();
            productCategory.setCategoryName("最佳科技榜单");
            productCategory.setCategoryType(1);
            productCategoryDao.save(productCategory);
        }
    
        /*测试更新*/
        @Test
        public void updateTest(){
            ProductCategory productCategory=new ProductCategory();
            productCategory.setCategoryId(2);
            productCategory.setCategoryName("科技榜单");
            productCategory.setCategoryType(10);
            productCategoryDao.save(productCategory);
        }
    
    
        @Test
        public void findByCategoryTypeInTest(){
            List<Integer>lists= Arrays.asList(1,2,3);
    
            List<ProductCategory> result=productCategoryDao.findByCategoryTypeIn(lists);
            Assert.assertNotEquals(0,result.size());
        }
    
    }
  • com.example.demo目录下创建包名service,并service下创建impl包,接着创建类CategoryService,CategoryServiceImpl
  • package com.example.demo.service;
    
    import com.example.demo.dataobject.ProductCategory;
    
    
    import java.util.List;
    
    public interface CategoryService {
    
        //根据Id查询
        public ProductCategory findOne(Integer caregoryId);
    
        //查询所有
        public List<ProductCategory>findAll();
    
        //根据编号的传入查询所有匹配数据
        public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
    
        //更新传入数据信息进行更新
        public ProductCategory save(ProductCategory productCategory);
    }
    
  •  
  • package com.example.demo.service.impl;
    
    import com.example.demo.dao.ProductCategoryDao;
    import com.example.demo.dataobject.ProductCategory;
    import com.example.demo.service.CategoryService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class CategoryServiceImpl implements CategoryService {
    
        @Autowired
        private ProductCategoryDao productCategoryDao;
    
        @Override
        public ProductCategory findOne(Integer caregoryId) {
            return productCategoryDao.findById(caregoryId).get();
        }
    
        @Override
        public List<ProductCategory> findAll() {
            return productCategoryDao.findAll();
        }
    
        @Override
        public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
            return productCategoryDao.findByCategoryTypeIn(categoryTypeList);
        }
    
        @Override
        public ProductCategory save(ProductCategory productCategory) {
            return productCategoryDao.save(productCategory);
        }
    }
    
  •  
  •   接着还是要进行必要测试
  • package com.wechat.sell.service.impl;
    
    import com.wechat.sell.dataobject.ProductCategory;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import static org.junit.Assert.*;
    
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class CategoryServiceImplTest {
    
        @Autowired
        private CategoryServiceImpl categoryService;
    
        @Test
        public void findOne() throws Exception{
            ProductCategory productCategory=categoryService.findOne(1);
            System.out.println("productCategory = " + productCategory);
            Assert.assertEquals(1,new Integer(1),productCategory.getCategoryId());
        }
    
        @Test
        public void findAll() throws Exception{
            List<ProductCategory>productCategoryList=categoryService.findAll();
            for(ProductCategory productCategory:productCategoryList){
                System.out.println("================ = " + "================");
                System.out.println("productCategory = " + productCategory);
            }
            Assert.assertNotEquals(0,productCategoryList.size());
        }
    
        @Test
        public void findByCategoryTypeIn() throws Exception{
            List<ProductCategory>productCategoryList=categoryService.findByCategoryTypeIn( Arrays.asList(1,2,3));
            for(ProductCategory productCategory:productCategoryList){
                System.out.println("================ = " + "================");
                System.out.println("productCategory = " + productCategory);
            }
            Assert.assertNotEquals(0,productCategoryList.size());
    
        }
    
        @Test
        public void save() throws Exception{
            ProductCategory productCategory=new ProductCategory();
            productCategory.setCategoryType(2);
            productCategory.setCategoryName("黑科技榜单");
            ProductCategory productCategory1=categoryService.save(productCategory);
            System.out.println("productCategory1 = " + productCategory1);
            Assert.assertNotNull(productCategory1);
        }
    }
  •  

博客提供有源码,源码中加入了Logback日志框架,源码可用于项目中套用并自行拓展

源码:源码下载

原创文章,未经允许,请勿盗用

发布了47 篇原创文章 · 获赞 28 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/weixin_44519467/article/details/105229950