Ա̳֮»Ա̳ վ Ѷ Լ--ʵս
б
¥: Գ

[] Լ--ʵս

2014-8-26 18:14:12

ͬhttp://www.waylau.com/convention-over-configuration-in-action/

Լһ򵥵ĸ ϵͳ⣬ӦüٶĬֵҪṩҪá ڴ󲿷£ᷢʹÿṩĬֵĿеĸ졣

òȫûãͨԼ, XML

Լṹ淶

ģиΪSale࣬ôݿжӦıͻĬΪsaleֻƫһԼʱ罫ñΪ"products_soldдйֵá

EJB3 ־ûһBean־ûҪֻǽעΪ @Entity ܽٶǻ ϵͳҲṩһЩӣҪʱдЩ.

maven ĿԼûԶ£Դٶ /workspace/content-zh/src/main/javaԴļٶ/workspace/content-zh/src/main/resources
Դٶ /workspace/content-zh/src/testĿٶһ JAR ļMavenٶҪѱõֽŵ /workspace/content-zh/target/classes /workspace/content-zh/target һɷַ JAR ļMaven ԼõӦòǼ򵥵Ŀ¼λãMaven ĺIJʹһͨõԼԴ룬ɷַĹ web վ㣬Ĺ̡ Maven "ϡһõںһ֪ιװͨòѭЩԼMaven ֻҪΪĹǽԴŵȷĿ¼Maven 㴦ʣµ顣

HTML5 Boilerplate,ΪAppĬģԼļ·淶վ߸UIAppԲģΪ𲽡HTML5 BoilerplateģIJֲ30УÿһжνǧСĽһЩǰ˵⣺

Boilerplate

ø÷ʽXML

hibernate.properties c3p0 ʽ:

  1. hibernate.connection.driver_class = org.postgresql.Driver
  2. hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
  3. hibernate.connection.username = myuser
  4. hibernate.connection.password = secret
  5. hibernate.c3p0.min_size=5
  6. hibernate.c3p0.max_size=20
  7. hibernate.c3p0.timeout=1800
  8. hibernate.c3p0.max_statements=50
  9. hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
ƴ

hibernate.properties JNDI ʽ

  1. hibernate.connection.datasource = java:/comp/env/jdbc/test
  2. hibernate.transaction.factory_class = \org.hibernate.transaction.JTATransactionFactory
  3. hibernate.transaction.manager_lookup_class = \ org.hibernate.transaction.JBossTransactionManagerLookup
  4. hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
ƴ

Apache Shiro ini

  1. [users]
  2. root = secret, admin
  3. guest = guest, guest
  4. presidentskroob = 12345, president
  5. darkhelmet = ludicrousspeed, darklord, schwartz
  6. lonestarr = vespa, goodguy, schwartz
  7. [roles]
  8. admin = *
  9. schwartz = lightsaber:*
  10. goodguy = winnebago:drive:eagle5
ƴ

Hibernateͨ

  1. Configuration cfg = new Configuration()
  2. .addClass(org.hibernate.auction.Item.class)
  3. .addClass(org.hibernate.auction.Bid.class)
  4. .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
  5. .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
  6. .setProperty("hibernate.order_updates", "true");
ƴ

Gradle Maven

Maven

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate-core</artifactId>
  4. <version>4.3.6.Final</version>
  5. </dependency>
ƴ

Gradle ֻһ

  1. org.hibernate:hibernate-core:4.3.6.Final
ƴ

ͨעԼ京

springע

SpringԶijЩ·µJava࣬ЩjavaעΪBeanʵʡȥ˽ bean XML á

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <context:component-scan base-package="com.waylau.rest"/>
  10. </beans>
ƴ

ע⣺ ô ǩͿԲxmlˣΪǰ߰˺ߡ ṩӱǩ ɨļĿ,

  1. <beans>
  2. <context:component-scan base-package="com.waylau.rest">
  3. <context:include-filter type="regex" expression=".*Stub.*Repository"/>
  4. <context:exclude-filter type="annotation"expression="org.springframework.stereotype.Repository"/>
  5. </context:component-scan>
  6. </beans>
ƴ

ʵ

  1. public static void main(String[] args) {
  2. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  3. ctx.scan("com.waylau.rest");
  4. ctx.refresh();
  5. MyService myService = ctx.getBean(MyService.class);
  6. }
ƴ

SpringʵĽʾָ·µȫעSpring Bean SpringͨʹAnnotationעBeanࡣ

  • @Component :עһͨSpring Beanࡣ
  • @Controller : עһࡣ
  • @Service : עһҵ߼ࡣ
  • @Repository : עһDAOࡣ

Hibernateע

ʵӳ

  1. @Entity
  2. @Table(name="TBL_FLIGHT",
  3. schema="AIR_COMMAND",
  4. uniqueConstraints=
  5. @UniqueConstraint(
  6. name="flight_number",
  7. columnNames={"comp_prefix", "flight_number"} ) )
  8. public class Flight implements Serializable {
  9. @Column(name="comp_prefix")
  10. public String getCompagnyPrefix() { return companyPrefix; }
  11. @Column(name="flight_number")
  12. public String getNumber() { return number; }
  13. }
ƴ

SQL Ҳע

  1. @Entity
  2. @Table(name="CHAOS")
  3. @SQLInsert( sql="INSERT INTO CHAOS(size, name, nickname, id) VALUES(?,upper(?),?,?)")
  4. @SQLUpdate( sql="UPDATE CHAOS SET size = ?, name = upper(?), nickname = ? WHERE id = ?")
  5. @SQLDelete( sql="DELETE CHAOS WHERE id = ?")
  6. @SQLDeleteAll( sql="DELETE CHAOS")
  7. @Loader(namedQuery = "chaos")
  8. @NamedNativeQuery(name="chaos", query="select id, size, name, lower( nickname ) as nickname from CHAOS where xml:id= ?", resultClass = Chaos.class)
  9. public class Chaos {
  10. @Id
  11. private Long id;
  12. private Long size;
  13. private String name;
  14. private String nickname;
  15. //...
  16. }
ƴ

Jersey 2.X ʵ REST MVC

  1. @POST
  2. @Produces({"text/html})
  3. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  4. @Template(name = "/short-link) @ErrorTemplate(name = "/error-form")
  5. @Valid
  6. public ShortenedLink createLink(@NotEmpty @FormParam("link") final String link) {
  7. // ...
  8. }
ƴ

Shiro ע

ûע

  1. //get the current Subject
  2. Subject currentUser =
  3. SecurityUtils.getSubject();
  4. if (currentUser.hasRole(administrator)) {
  5. //do something in here that only a administrator?
  6. } else {
  7. //dont do ?
  8. }
ƴ

ע⣬ܶ

  1. @RequiresRoles( administrator )
  2. public void openAccount( Account acct ) {
  3. //do something in here that only a administrator
  4. }
ƴ

Struts 2.x ע

  1. package com.waylau.actions;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import org.apache.struts2.convention.annotation.Action;
  4. import org.apache.struts2.convention.annotation.Actions;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. @Results({
  8. @Result(name="failure", location="fail.jsp")
  9. })
  10. public class HelloWorld extends ActionSupport {
  11. @Action(value="/different/url",
  12. results={@Result(name="success", location="http://struts.apache.org", type="redirect")}
  13. )
  14. public String execute() {
  15. return SUCCESS;
  16. }
  17. @Action("/another/url")
  18. public String doSomething() {
  19. return SUCCESS;
  20. }
  21. }
ƴ

ο

  • Spring
  • Hibernate
  • Jersey
  • Apache Shiro
  • Apache Struts
  • http://www.waylau.com/build-java-project-with-maven/
  • https://github.com/waylau/Gradle-2-User-Guide