美文网首页
Good Thymes Virtual Grocery with

Good Thymes Virtual Grocery with

作者: OREOs | 来源:发表于2016-11-09 19:00 被阅读0次

课程地址:http://ss.sysu.edu.cn/~pml/dct/9_data_access.html

JDBC实例:http://ss.sysu.edu.cn/~pml/dct/_downloads/jdbc-basic.rar
JPA实例:http://ss.sysu.edu.cn/~pml/dct/_downloads/jdbc-jpa.rar

JPA简单讲解http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/index.html

源代码 Spring Thyme Seed Starter Manager:https://github.com/thymeleaf/thymeleafexamples-stsm

添加依赖

<!-- spring -->
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
    </dependency>

    <!-- persistence -->
<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-connector-java.version}</version>
    </dependency>

    <!-- utils -->
    <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
    </dependency>

    <!-- data source -->
    <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-dbcp</artifactId>
            <version>${datasource-dbcp.version}</version>
    </dependency>
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
</dependency>

准备数据访问属性文件

# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_jpa?createDatabaseIfNotExist=true&useSSL=false
jdbc.user=root
jdbc.pass=root

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop

准备配置文件,这里使用java配置

根据给出的jdbc-jpa案例,因为在原来的grocery基础上作调整,所以改变了工程的结构,同时也要满足

com.your-company.project.application
com.your-company.project.persistence.dao
com.your-company.project.persistence.service
com.your-company.project.persistence.entity
com.your-company.project.web.controller

这些包的格式要求

@Configuration
@ComponentScan({ "thymeleafexample.gtvg" })
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "thymeleafexample.gtvg.business.entities" });

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }

http://blog.csdn.net/lyq123333321/article/details/44217409

@Column注释定义了将成员属性映射到关系表中的哪一列和该列的结构信息,属性如下:

1)name:映射的列名。如:映射tbl_user表的name列,可以在name属性的上面或getName方法上面加入;

2)unique:是否唯一;

3)nullable:是否允许为空;

4)length:对于字符型列,length属性指定列的最大字符长度;

5)insertable:是否允许插入;

6)updatetable:是否允许更新;

7)columnDefinition:定义建表时创建此列的DDL;

8)secondaryTable:从表名。如果此列不建在主表上(默认是主表),该属性定义该列所在从表的名字。

@Id注释指定表的主键,它可以有多种生成方式:

1)TABLE:容器指定用底层的数据表确保唯一;

2)SEQUENCE:使用数据库德SEQUENCE列莱保证唯一(Oracle数据库通过序列来生成唯一ID);

3)IDENTITY:使用数据库的IDENTITY列莱保证唯一;

4)AUTO:由容器挑选一个合适的方式来保证唯一;

5)NONE:容器不负责主键的生成,由程序来完成。

@GeneratedValue注释定义了标识字段生成方式。

@Temporal注释用来指定java.util.Date或java.util.Calender属性与数据库类型date、time或timestamp中的那一种类型进行映射。

@Temporal(value=TemporalType.TIME)

因为改了数据库的登录密码,所以把properties中的密码改掉

# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_jpa?createDatabaseIfNotExist=true&useSSL=false
jdbc.user=root
jdbc.pass=123

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop

https://github.com/OREOmini/thymeleafexamples-gtvg-jpa

相关文章

网友评论

      本文标题:Good Thymes Virtual Grocery with

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