idea常用模板
Categories: 杂文
文章目录[隐藏]
- 1.hibernate映射文件:hibernate-mapping.xml(实体类同包)
- 2.hibernate配置文件:hibernate-cfg.xml(resources资源文件夹)
- 3.jdbc配置文件:jdbc.properties(resources资源文件夹)
- 4.log4j配置文件:log4j.properties(resources资源文件夹)
- 5.spring配置文件:spring-config.xml(resources资源文件夹)
- 6.springmvc配置文件:springmvc-servlet.xml(webapp/web-inf文件夹)
- 7.web配置文件:web.xml(webapp/web-inf文件夹)
Settings --> Editor --> File and Code Templates
1.hibernate映射文件:hibernate-mapping.xml(实体类同包)
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- class的name是实体类的全路径,table是数据库的表名 -->
<class name="" table="">
<!-- id标签用于设置主键,name是实体类中的属性名,type是属性类型 -->
<id name="" type="">
<!-- column代表数据库中的字段,name是字段名,precision有效位数,scale精度-->
<column name="" precision="" scale=""/>
<!-- generator主键生成策略 -->
<generator class="identity"/>
</id>
<!-- property是普通的属性,name是实体类中的属性名,type是属性类型 -->
<property name="" type="">
<!-- column代表数据库中的字段,name是字段名,length是长度-->
<column name="" length=""/>
</property>
</class>
</hibernate-mapping>
2.hibernate配置文件:hibernate-cfg.xml(resources资源文件夹)
<?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>
<!-- mysql连接URL -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8</property>
<!-- mysql驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql账户名 -->
<property name="connection.username">root</property>
<!-- mysql密码 -->
<property name="connection.password"></property>
<!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- 格式化sql语句 -->
<property name="format_sql">true</property>
<mapping resource="pojo/Dept.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.jdbc配置文件:jdbc.properties(resources资源文件夹)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://数据库地址:3306/数据库?characterEncoding=utf-8
jdbc.username=用户名
jdbc.password=密码
4.log4j配置文件:log4j.properties(resources资源文件夹)
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
5.spring配置文件:spring-config.xml(resources资源文件夹)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!--读取jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!--扫描映射文件所在的包:实体类包-->
<property name="mappingLocations" value="classpath:com/pojo/*.hbm.xml"/>
</bean>
<!--批量扫描生成bean(dao和Service):com包下所有文件-->
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--如何进行事务增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="update*" rollback-for="Exception"/>
<tx:method name="delete*" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--定义需要增强的方法:service包-->
<aop:pointcut id="adviceMethod" expression="execution(* com.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="adviceMethod"/>
</aop:config>
</beans>
6.springmvc配置文件:springmvc-servlet.xml(webapp/web-inf文件夹)
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<!--开启注解-->
<mvc:annotation-driven/>
<!--扫描Controller:controller包-->
<context:component-scan base-package="com.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
7.web配置文件:web.xml(webapp/web-inf文件夹)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--加载springMVC配置文件-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--跟随tomcat启动同时加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--编码过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<!--强制编码转换-->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<!--过滤所有访问-->
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--处理懒加载-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<!--访问默认路径启动页面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>