持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情
添加相关配置
(1) MyBatis 配置
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>\
<!DOCTYPE configuration\
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"\
"http://mybatis.org/dtd/mybatis-3-config.dtd">\
<configuration>
<settings>\
<setting name="logImpl" value="STDOUT_LOGGING"/>\
</settings>\
<typeAliases>\
<package name="com.bjpowernode.crm.model"/>\
</typeAliases>\
<mappers>\
<package name="com.bjpowernode.crm.mapper"/>\
</mappers>\
</configuration>
(2) 配置数据连接和事务
applicationContext-datasource.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: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.3.xsd\
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd\
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">\
<!-- 配置数据源 -->\
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">\
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>\
<property name="username" value="root"/>\
<property name="password" value="123456"/>\
<property name="url" value="jdbc:mysql://192.168.223.133:3306/crm_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>\
</bean>\
<!-- 配置SqlSessionFactory -->\
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">\
<!-- 必须注入属性dataSource -->\
<property name="dataSource" ref="dataSource"/>\
<!-- 如果mybatis没有特殊的配置(比如别名等),configLocation可以省去 ;否则,不能省略-->\
<property name="configLocation" value="classpath:mybatis-config.xml"/>\
</bean>\
<!-- mapper注解扫描器配置,扫描@MapperScan注解,自动生成代码对象 -->\
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">\
<property name="basePackage" value="com.bjpowernode.crm.mapper"/>\
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>\
</bean>\
\
<!-- 配置事务管理器 -->\
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">\
<property name="dataSource" ref="dataSource"/>\
</bean>\
<!-- 配置事务 -->\
<aop:config>\
<aop:pointcut expression="execution(* com.bjpowernode.crm..service.*.*(..))" id="allMethodPointcut"/>\
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethodPointcut"/>\
</aop:config>\
<tx:advice id="txAdvice" transaction-manager="transactionManager">\
<tx:attributes>\
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception"/>\
<tx:method name="*" propagation="REQUIRED" read-only="true"/>\
</tx:attributes>\
</tx:advice>\
</beans>
(3) springmvc 配置
applicationContext-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
(4) spring 总配置文件
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载系统配置文件
<context:property-placeholder location="classpath:*.properties" />-->
<!-- 扫描注解 -->
<context:component-scan base-package="com.bjpowernode.crm.service" />
<!-- 导入数据相关配置 -->
<import resource="applicationContext-datasource.xml" />
</beans>
(5) web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>\
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
xmlns="http://java.sun.com/xml/ns/javaee"\
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee\
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"\
id="dataservice" version="3.0">\
<display-name>dataservice application</display-name>\
<!-- spring监听器加载applicationContext.xml配置文件 -->\
<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>\
<!-- spring字符过滤器 -->\
<filter>\
<filter-name>encodingFilter</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>\
</filter>\
<filter-mapping>\
<filter-name>encodingFilter</filter-name>\
<url-pattern>/*</url-pattern>\
</filter-mapping>\
<!-- Spring mvc分发servlet -->\
<servlet>\
<servlet-name>dispatcher</servlet-name>\
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>\
<init-param>\
<param-name>contextConfigLocation</param-name>\
<param-value>classpath:applicationContext-mvc.xml</param-value>\
</init-param>\
<load-on-startup>1</load-on-startup>\
</servlet>\
<servlet-mapping>\
<servlet-name>dispatcher</servlet-name>\
<url-pattern>/</url-pattern>\
</servlet-mapping>\
<servlet-mapping>\
<servlet-name>dispatcher</servlet-name>\
<url-pattern>*.do</url-pattern>\
</servlet-mapping>\
<!-- 欢迎页,默认进入index controller -->\
(6) 设置maven对配置文件的编译选项
pom.xml
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
添加页面及静态资源
commons/
css/
images/
img/
js/
*.jsp
创建数据持久层接口及映射文件
采用mybatis的逆向工程。
1. 创建mybatis的逆向工程
1.1 创建maven版的java工程
1.2 添加mybatis逆向工程插件
<!--myBatis逆向工程插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
1.3 提供mybatis逆向工程配置文件
generatorConfig.xml