Spring Data Development Environment Construction (II.)

Source: Internet
Author: User

First, let's create a MAVEN project.

Join the following dependencies in Pom.xml

<!--Mysql Driver Pack -    <Dependency>      <groupId>Mysql</groupId>      <Artifactid>Mysql-connector-java</Artifactid>      <version>5.1.38</version>    </Dependency><!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.DATA/SPRING-DATA-JPA -    <Dependency>      <groupId>Org.springframework.data</groupId>      <Artifactid>Spring-data-jpa</Artifactid>      <version>1.11.6.RELEASE</version>    </Dependency>    <!--Https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -    <Dependency>      <groupId>Org.hibernate</groupId>      <Artifactid>Hibernate-entitymanager</Artifactid>      <version>5.2.9.Final</version>    </Dependency>

Then configure the spring file

Create a bean.xml<? XML version= "1.0" encoding= "UTF-8" ?>

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:JPA= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/ Spring-jpa-1.3.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd ">    <!--Configure the data source -    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>        < Propertyname= "username"value= "root"/>        < Propertyname= "Password"value= "123"/>        < Propertyname= "url"value= "Jdbc:mysql:///springdata"/>    </Bean>    <!--Configure Entitymanagerfactory -    <BeanID= "Entitymanagerfactory"class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        < Propertyname= "DataSource"ref= "DataSource"/>        < Propertyname= "Jpavendoradapter">            <Beanclass= "Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>        </ Property>        < Propertyname= "Packagestoscan"value= "Org.springdata"/>        < Propertyname= "Jpaproperties">            <Props>                <propKey= "Hibernate.ejb.naming_strategy">Org.hibernate.cfg.ImprovedNamingStrategy</prop>                <propKey= "Hibernate.dialect">Org.hibernate.dialect.MySQL5InnoDBDialect</prop>                <propKey= "Hibernate.show_sql">True</prop>                <propKey= "Hibernate.format_sql">True</prop>                <propKey= "Hibernate.hbm2ddl.auto">Update</prop>            </Props>        </ Property>    </Bean>    <!--3 Configuring the transaction manager -    <BeanID= "TransactionManager"class= "Org.springframework.orm.jpa.JpaTransactionManager">        < Propertyname= "Entitymanagerfactory"ref= "Entitymanagerfactory"/>    </Bean>    <!--4 Configuring a transaction that supports annotations -    <Tx:annotation-drivenTransaction-manager= "TransactionManager"/>    <!--5 Configuring Spring Data -    <jpa:repositoriesBase-package= "Org.springdata"Entity-manager-factory-ref= "Entitymanagerfactory"/>    <Context:component-scanBase-package= "Org.springdata"/></Beans>

Create an entity class Employee.java we can automatically generate data tables with JPA annotations

  

 PackageOrg.springdata.domain;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id;/*** Employee: First Develop entity class ===> automatically generate data table*/@Entity Public classEmployee {PrivateInteger ID; PrivateString name; PrivateInteger age; @GeneratedValue @Id PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; } @Column (Length= 20)     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

 PackageOrg.springdata;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Test Class*/ Public classSpringdatatest {PrivateApplicationContext CTX =NULL; @Before Public voidSetup () {CTX=NewClasspathxmlapplicationcontext ("Beans.xml"); System.out.println ("Setup"); } @After Public voidTearDown () {CTX=NULL; System.out.println ("TearDown"); } @Test Public voidtestentitymanagerfactory () {}}

Create a test class as shown on the green horizontal bar indicates success, in the database to see if the successful creation of an employee table

All right, the environment is here. Is it easy to focus on walking and living to 99 old iron?

Spring Data Development Environment Construction (II.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.