略
Hibernate
hibernate.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hb?useUnicode=true&characterEncoding=UTF-8 </property> <property name="hibernate.default_schema">hb</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.autocommit">false</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.timeout">120</property> <property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property>
<mapping resource="entity/XXX.hbm.xml"/>
</session-factory> </hibernate-configuration>
|
XXX.hbm.xml
技巧: 级联的时候思考下,是否符合业务逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping schema="数据库库名" catalog="目录名称" default-cascade="级联风格" default-access="访问策略" default-lazy="加载策略" package="包名" auto-import="自动导入"> <class name="Entry类名" table="表名" schema="数据库库名" batch-size="抓取策略" where="条件" entity-name="同实体类映射多个表(一般很少用,一般是一个类对应一张表)"> <id name="类字段名(主键)" type="数据类型" column="列名" length="长度"> <generator class="主键生成策略"/> <column name="数据库列名" sql-type="该列对应的类型"/> </id> <property name="类字段名(普通列)"> <column name="数据库列名" sql-type="该列对应的类型" length="长度"/> </property> <property name="类字段名(普通列)"> <column name="数据库列名" sql-type="该列对应的类型" length="长度" not-null="是否为空"/> </property>
<set name="students" inverse="true" cascade="delete"> <key column="gid"/> <one-to-many class="com.yuda.entity.Student"/> </set>
<many-to-one name="gradeByGid" class="com.yuda.entity.Grade" cascade="save-update"> <column name="gid"/> </many-to-one>
<component name="实体类中的引用类型"> <property name="类字段名(普通列)"> <column name="数据库列名" sql-type="该列对应的类型" length="长度"/> </property> </component>
</class> </hibernate-mapping>
|
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.yuda.entity.Grade" table="grade" schema="hb"> <id name="gid"> <column name="gid" sql-type="int(11)"/> <generator class="identity"/> </id> <property name="gname"> <column name="gname" sql-type="varchar(20)" length="20"/> </property> <property name="gdesc"> <column name="gdesc" sql-type="varchar(50)" length="50" not-null="true"/> </property> <set name="students" inverse="true" cascade="delete"> <key column="gid"/> <one-to-many class="com.yuda.entity.Student"/> </set> </class> </hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.yuda.entity">
<class name="Student" table="student" schema="hb">
<id name="sid"> <column name="sid" sql-type="int(11)"/> <generator class="native"/> </id> <property name="sname"> <column name="sname" sql-type="varchar(20)" length="20"/> </property> <property name="sex"> <column name="sex" sql-type="char(2)" length="2" not-null="true"/> </property> <many-to-one name="gradeByGid" class="com.yuda.entity.Grade" cascade="save-update"> <column name="gid"/> </many-to-one> </class> </hibernate-mapping>
|
注解的使用(代替xml的存在,注意导包):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| @Entity @Table(name = "province",schema = "china") public class Province {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int p_id;
@Column(name = "p_name",length = 30,nullable = false) private String p_name;
@OneToMany(targetEntity = City.class,mappedBy = "province") @Cascade(CascadeType.SAVE_UPDATE) private Set<City> cities = new HashSet<>();
}
@Entity @Table(name = "city",schema = "china") public class City {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int c_id;
@Column(name = "c_name",length = 30,nullable = false) private String c_name;
@JSONField(serialize = false) @ManyToOne(targetEntity = Province.class) @JoinColumn(name = "p_id") @Cascade(CascadeType.SAVE_UPDATE) private Province province;
@OneToMany(targetEntity = Area.class,mappedBy = "city") @Cascade(CascadeType.SAVE_UPDATE) private Set<Area> areas = new HashSet<>();
}
@Entity @Table(name = "area",schema = "china") public class Area { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int a_id;
@Column(name = "a_name",length = 30,nullable = false) private String a_name;
@JSONField(serialize = false) @ManyToOne(targetEntity = City.class) @JoinColumn(name = "c_id") @Cascade(CascadeType.SAVE_UPDATE) private City city; }
|
注意:
1 2 3 4 5 6 7 8
| @Id @Column(name = "b_id") @GenericGenerator(name = "my_uuid", strategy = "uuid") @GeneratedValue(generator = "my_uuid") public Integer getId() { return id; }
|
Struts2
web.xml
1 2 3 4 5 6 7 8 9 10 11 12
|
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
|
struts.xml
技巧: 可以创建一个主struts.xml文件,内部使用标签来导入其他的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"/> <constant name="struts.i18n.encoding" value="utf-8"/>
<package name="名字" namespace="/" extends="struts-default">
<interceptors> <interceptor name="loginInterceptor" class="com.yuda.utils.LoginInterceptor"/> <interceptor name="myInterceptor" class="com.yuda.utils.MyInterceptor"/> <interceptor-stack name="myStack"> <interceptor-ref name="defaultStack"/> <interceptor-ref name="myInterceptor"/> <interceptor-ref name="loginInterceptor"/> </interceptor-stack> </interceptors>
<global-results> <result name="success" type="跳转方式(主要用的有4种)"></result> </global-results> <action name="url組成部分" class="com.yuda.Struts2Action" method="show"> <result name="success" type="跳转方式">/html/success.html</result> <result name="error">/html/failer.html</result> </action> </package> </struts>
|
使用注解(代替xml的存在):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| @Namespace(父url) @ParentPackage(包名) public class ProductAction extends ActionSupport { @Action(value = "showProduct",interceptorRefs = {@InterceptorRef(value = "myStack")}) public void showProduct(){ ServletActionContext.getResponse().setCharacterEncoding("utf-8"); IProductService productService = new ProductServiceImpl(); List<Product> ps = productService.findAll();
Result<Product> result = new Result<>(); result.setContent(ps); result.setType(1); String json = JSONArray.toJSONString(result); try { ServletActionContext.getResponse().getWriter().write(json); } catch (IOException e) { e.printStackTrace(); } } }
@Namespace("/") @ParentPackage("struts-default")
public class UserAction extends ActionSupport implements ModelDriven<User> { @Action(value = "user_login",results = { @Result(name = "success",location = "/product.jsp"), @Result(name = "error",location = "/login.jsp") }) public String login(){
IUserService service = new UserService();
User login = service.login(user);
if (login == null){ return ERROR; } else { ServletActionContext.getRequest().getSession().setAttribute("user",login); return SUCCESS; } }
private User user = new User();
@Override public User getModel() { return user; } }
|
Spring
Spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" profile="dev"> <context:annotation-config/>
<bean class = "类路径"/> <import resource = "XXX.xml"/> <context:property-placeholder location="classpath*:c3p0.properties"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClass}" p:url="${jdbc.jdbcUrl}" p:username="${jdbc.user}" p:password="${jdbc.password}"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="account" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.yuda.IAccountService.account(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <util:list id="my_list"> <value>1</value> <value>1</value> <value>1</value> <value>1</value> <value>1</value> </util:list> <bean id="peppers" class="com.yuda.component.SgtPeppers" init-method="toString" destroy-method="toString"/>
<bean id="peppers2" class="com.yuda.component.SgtPeppers" scope="prototype"/>
<bean id="cdplayer1" class="com.yuda.component.CDPlayer"> <constructor-arg ref="peppers"/> </bean> <bean id="cdplayer2" class="com.yuda.component.CDPlayer" p:cd-ref="peppers"/>
<bean id="cdplayer3" class="com.yuda.component.CDPlayer"> <property name="cd" ref="peppers"/> </bean>
<bean id="blank" class="com.yuda.component.BlankDisc" c:title="aaa" c:artist="bbb" c:tracks-ref="my_list"> </bean>
</beans>
|
Spring使用c3p0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="30"/>
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="60"/>
<property name="acquireIncrement" value="5"/>
<property name="maxStatements" value="0"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="breakAfterAcquireFailure" value="true"/>
<property name="testConnectionOnCheckout" value="false"/> </bean>
|
Spring-aop相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config expose-proxy="true" proxy-target-class="true">
<aop:pointcut id="test_point_cut" expression="execution(* com.yuda.component.MyTest.*(..))"/>
<aop:aspect ref="my_advice">
<aop:before method="before" pointcut-ref="test_point_cut"/>
<aop:after method="after" pointcut-ref="test_point_cut"/>
<aop:around method="around" pointcut-ref="test_point_cut"/>
<aop:after-returning method="afterReturning" pointcut-ref="test_point_cut" returning="val"/>
</aop:aspect> </aop:config>
|
Spring加入web
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:Spring-ioc.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
|
log4j
log4j.properties
1 2 3 4 5
| log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=warn, stdout
|
maven
settings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>D:\apache-maven-3.1.1\repository</localRepository> <pluginGroups></pluginGroups> <proxies></proxies> <servers></servers> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>jdk-1.8</id> <activation> <jdk>1.8</jdk> <activeByDefault>true</activeByDefault> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> </profiles> </settings>
|
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
|