一.准备:使用maven创建module或项目,确保MySQL可以连接和使用
二.开始
1.添加新的目录结构:
2.开始导入依赖:包含ssh和其他一些包,沿用MyEclipse使用的ssh包,有点多,ssh的版本比较前中期的,自己学习开发点东西应该是够的。右侧maven下的dependencies 如果有出现红色波浪线,参考
依赖配置:ssh依赖配置
3.数据库配置:(先进入计算机的服务管理,先把mysql的服务开启来)
idea右侧的DataBase点击一下,点击“+”号进入下图界面,配置user,password、DataBase,下载一下驱动,Test Connection
ok,一般连接是失败的,时区错误,别担心,很容易解决,可参考,改时区一看就很复杂,所以直接改一波驱动应该就可以了。
以上,依赖,数据库配置均已完成,然后:
3. 配置web.xml ,
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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" metadata-complete="false" >
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<!--
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
struts2版本2.5以上用下面的,之前的用上面的
-->
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.applicationContext.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.lcj"></context:component-scan>
<!-- 配置数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/idea"></property>
<property name="idleConnectionTestPeriod" value="100"></property>
<property name="preferredTestQuery" value="select * from user"></property>
<property name="testConnectionOnCheckin" value="true"></property>
<property name="testConnectionOnCheckout" value="true"></property>
</bean>
<!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/idea</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<!--
<property name="mappingLocations" value="classpath:com/lcj/model/*.hbm.xml"></property>
-->
<!-- 包扫描的方式加载注解类 -->
<property name="packagesToScan">
<list>
<value>com.lcj.model</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>com.lcj.model.User</value>
</list>
</property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="login" propagation="REQUIRES_NEW"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRES_NEW"/>
<tx:method name="update*" propagation="REQUIRES_NEW"/>
<tx:method name="delete*" propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.lcj.service.*.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
*5. struts.xml和db.properties()
5.1
<?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>
<package name="userActions" namespace="/usertodo" extends="struts-default">
<action name="login" class="com.lcj.controller.UserAction" method="userLogin">
<result name="success" >
/index.jsp
</result>
<result name="failed" >
/login.jsp
</result>
</action>
</package>
</struts>
5.2
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/idea?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
配置文件基本完成。
git项目代码:
参考:https://blog.youkuaiyun.com/qq_37677519/article/details/77149993