美文网首页JAVA_Spring
SpringBoot 11 根据bean逆向生成表

SpringBoot 11 根据bean逆向生成表

作者: kason_zhang | 来源:发表于2018-11-03 12:24 被阅读0次

    Springboot 通过配置hibernate相关可以实现根据model bean来逆向生成数据库表, 此时如果使用默认的mysql方言org.hibernate.dialect.MySQL5Dialect则会出现创建的mysql数据库表是latin的格式, 无法写入中文, 此时就需要复写MySQL5Dialect类

    package com.secondkill.db;
    
    import org.hibernate.dialect.MySQL5InnoDBDialect;
    
    /**
     * Created by zhangkai12 on 2018/7/9.
     */
    public class MySQL5DialectUTF8 extends MySQL5InnoDBDialect {
    
        @Override
        public String getTableTypeString() {
            return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
        }
    
    }
    

    之后再application.properties中添加逆向生成数据库表的配置信息:

    spring.datasource.url=jdbc:mysql://localhost:3306/secondkill?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.show-sql= true
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.properties.hibernate.dialect=com.secondkill.db.MySQL5DialectUTF8
    

    之后编写entity bean, 比如此处编写一个Goods类:

    package com.secondkill.entity;
    
    import javax.persistence.*;
    
    @Entity
    @Table(name = "goods")
    public class Goods {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
        @Column
        private String goodsName;
        @Column
        private String goodsTitle;
        @Column
        private String goodsImg;
        @Column
        private String goodsDetail;
        @Column
        private Double goodsPrice; // 商品价格
        @Column
        private Integer goodsStock; // 商品库存
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getGoodsName() {
            return goodsName;
        }
        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
        public String getGoodsTitle() {
            return goodsTitle;
        }
        public void setGoodsTitle(String goodsTitle) {
            this.goodsTitle = goodsTitle;
        }
        public String getGoodsImg() {
            return goodsImg;
        }
        public void setGoodsImg(String goodsImg) {
            this.goodsImg = goodsImg;
        }
        public String getGoodsDetail() {
            return goodsDetail;
        }
        public void setGoodsDetail(String goodsDetail) {
            this.goodsDetail = goodsDetail;
        }
        public Double getGoodsPrice() {
            return goodsPrice;
        }
        public void setGoodsPrice(Double goodsPrice) {
            this.goodsPrice = goodsPrice;
        }
        public Integer getGoodsStock() {
            return goodsStock;
        }
        public void setGoodsStock(Integer goodsStock) {
            this.goodsStock = goodsStock;
        }
    }
    

    运行Springboot项目之后, 就可以到数据库中查看最新生成的表:


    image.png

    编写的Mapper

    package com.secondkill.dao;
    
    import com.secondkill.entity.Goods;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    
    /**
     * Created by zhangkai12 on 2018/7/9.
     */
    @Mapper
    public interface GoodsMapper {
    
        @Insert("insert into goods (goods_name, goods_title, goods_img, goods_detail, goods_price, goods_stock) " +
                "values (#{goodsName}, #{goodsTitle}, #{goodsImg}, #{goodsDetail}, #{goodsPrice}, #{goodsStock})")
        public void insertGoods(Goods goods);
    }
    
    package com.secondkill.dao;
    
    import com.secondkill.entity.MiaoshaGoods;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    
    /**
     * Created by zhangkai12 on 2018/7/9.
     */
    @Mapper
    public interface MiaoshaGoodsMapper {
    
        @Insert("insert into miaosha_goods (goods_id, start_date, end_date, stock_count) values (#{goodsId}, #{startDate}, #{endDate}, #{stockCount})")
        public void insertMiaoShaGoods(MiaoshaGoods miaoshaGoods);
    }
    

    Test类:

    package com.secondkill.dao;
    
    import com.secondkill.SecondkillApplicationTests;
    import com.secondkill.entity.Goods;
    import com.secondkill.entity.MiaoshaGoods;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.util.Calendar;
    import java.util.Date;
    
    import static org.junit.Assert.*;
    
    /**
     * Created by zhangkai12 on 2018/7/9.
     */
    public class MapperTest extends SecondkillApplicationTests {
        @Autowired
        GoodsMapper goodsMapper;
        @Autowired
        MiaoshaGoodsMapper miaoshaGoodsMapper;
        @Test
        public void insertGoods() throws Exception {
            Goods goods = new Goods();
            goods.setGoodsDetail("oppo 手机");
            goods.setGoodsImg("http://");
            goods.setGoodsName("oppo phone");
            goods.setGoodsPrice(1998.0);
            goods.setGoodsStock(10);
            goods.setGoodsTitle("oppo");
    
            goodsMapper.insertGoods(goods);
        }
    
        @Test
        public void testInsertMiaoShaGoods() throws Exception {
            MiaoshaGoods miaoshaGoods = new MiaoshaGoods();
            miaoshaGoods.setGoodsId(1L);
            miaoshaGoods.setStockCount(2);
            Calendar c = Calendar.getInstance();
            c.set(2018 , 7, 8,8, 0);
    
            miaoshaGoods.setStartDate(c.getTime());
            c.set(2018, 7,10,0,0);
            miaoshaGoods.setEndDate(c.getTime());
            miaoshaGoodsMapper.insertMiaoShaGoods(miaoshaGoods);
    
        }
    
    }
    

    相关文章

      网友评论

        本文标题:SpringBoot 11 根据bean逆向生成表

        本文链接:https://www.haomeiwen.com/subject/nbktpftx.html