SSM+druid开发配置
工程目录
1、先从pom文件开始吧
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yangpei</groupId> <artifactId>RedLemon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
<name>RedLemon</name> <url>https://home.cnblogs.com/u/InterfaceAOP/</url> <inceptionYear>2019-2020</inceptionYear>
<!-- 项目属性 --> <properties> <spring-version>4.1.9.RELEASE</spring-version> <mybatis.version>3.2.8</mybatis.version> <mybatis-spring.version>1.2.3</mybatis-spring.version> <druid.version>1.1.10</druid.version> <!-- tomcat --> <tomcat.version>2.2</tomcat.version> <webserver.port>8181</webserver.port> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<dependencies> <!-- druid配置 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency>
<!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency>
<!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring-version}</version> </dependency>
<!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency>
</dependencies>
<!-- 开发者信息 --> <developers> <!-- <developer> <id>LonelyPerson</id> <name>MaYangPei</name> <email>1582240996@qq.com</email> <roles> <role>Project LonelyPerson</role> </roles> <timezone>+8</timezone> </developer> --> </developers> <build> <plugins> <!-- JDK版本变动解决方案 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- tomcat7插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>${tomcat.version}</version> <configuration> <port>${webserver.port}</port> <path>/</path> <uriEncoding>${project.build.sourceEncoding}</uriEncoding> </configuration> </plugin> </plugins> </build> </project> |
lonelyPerson.properties配置文件
jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/lonelyperson?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root
#druid datasource druid.initialSize=10 druid.minIdle=10 druid.maxActive=50 druid.maxWait=60000 druid.timeBetweenEvictionRunsMillis=30000 druid.minEvictableIdleTimeMillis=150000 druid.validationQuery=SELECT 'x' druid.validationQueryTimeout=3600 druid.testWhileIdle=true druid.testOnBorrow=false druid.testOnReturn=false druid.removeAbandoned=true druid.removeAbandonedTimeout=1800000 druid.logAbandoned=true druid.poolPreparedStatements=true druid.maxPoolPreparedStatementPerConnectionSize=20 druid.filters=wall,stat |
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"> <!-- 全局参数 --> <settings> <!-- 使全局的映射器启用或禁用缓存。 --> <setting name="cacheEnabled" value="true"/>
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 --> <setting name="lazyLoadingEnabled" value="true"/>
<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 --> <setting name="aggressiveLazyLoading" value="true"/>
<!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true --> <setting name="multipleResultSetsEnabled" value="true"/>
<!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true --> <setting name="useColumnLabel" value="true"/>
<!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false --> <setting name="useGeneratedKeys" value="false"/>
<!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 --> <setting name="autoMappingBehavior" value="PARTIAL"/>
<!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) --> <setting name="defaultExecutorType" value="SIMPLE"/>
<!-- 使用驼峰命名法转换字段。 --> <setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session --> <setting name="localCacheScope" value="SESSION"/>
<!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 --> <setting name="jdbcTypeForNull" value="NULL"/>
</settings> |
spring-context.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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 启动包扫描功能 --> <!-- <context:component-scan base-package="com.yangpei.controller" /> <context:component-scan base-package="com.yangpei.service" /> -->
<!-- 加载配置属性文件 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:lonelyPerson.properties" />
<!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置监控统计拦截的filters --> <!-- <property name="filters" value="stat" /> --> <!-- 连接池最大值 --> <property name="maxActive" value="${druid.maxActive}" /> <!-- 初始化大小 --> <property name="initialSize" value="${druid.initialSize}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${druid.maxWait}" /> <!-- 连接池最小空闲 --> <!-- <property name="minIdle" value="1" /> --> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" /> <property name="validationQuery" value="${druid.validationQuery}" /> <property name="validationQueryTimeout" value="${druid.validationQueryTimeout}" /> <property name="testWhileIdle" value="${druid.testWhileIdle}" /><!--空闲时是否进行验证,检查对象是否有效 --> <property name="testOnBorrow" value="${druid.testOnBorrow}" /><!--取得对象时是否进行验证,检查对象是否有效 --> <property name="testOnReturn" value="${druid.testOnReturn}" /><!--返回对象时是否进行验证 --> <property name="removeAbandoned" value="${druid.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}" /> <property name="logAbandoned" value="${druid.logAbandoned}" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 oracle=true mysql=false --> <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" /><!-- 表明是否开启statement cache,默认为false,也就是不开启 --> <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxPoolPreparedStatementPerConnectionSize}" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="${druid.filters}" /><!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 --> </bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean>
<!-- session 工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 用于连接数据库的数据源 --> <property name="dataSource" ref="dataSource" /> <!-- 指定Mybatis的配置文件位置 --> <!-- <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations"> <list> <value>classpath*:mapper/*Mapper.xml</value> </list> </property> --> </bean> <!-- 管理mybatismapper.xml文件 --> <!-- <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yangpei.**.mapper" /> </bean> -->
<!-- 开启事务管理 --> <!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描的包路径,如果需要扫描多个包,中间使用逗号分隔 --> <property name="basePackage" value="com.yangpei.**.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans> |
spring-servlet.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启动注解驱动的spring mvc 功能 --> <mvc:annotation-driven/> <!-- <mvc:default-servlet-handler/> 放行资源的第三种方式 --> <!-- 启动包扫描功能 --> <context:component-scan base-package="com.yangpei.controller" /> <context:component-scan base-package="com.yangpei.service" />
<mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/bootstrap3.3.5/" mapping="/bootstrap3.3.5/**"/> <mvc:resources location="/assets/" mapping="/assets/**"/> <mvc:resources location="/ztree/" mapping="/ztree/**"/> <!-- <mvc:resources location="/WEB-INF/views/order/" mapping="/order/**"/> -->
<!-- 配置全局异常类 --> <!-- <bean class="com.haoyu.common.SpringExceptionResolver"></bean> --> <!-- 配置全局上下文 --> <!-- <bean id="application" class="com.haoyu.common.ApplicationContextHelper"></bean> --> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <!-- 配置json解析器 --> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> <!-- 默认视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".html" /> </bean>
</beans> |
Web.xml文件
<?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_4_0.xsd" version="4.0"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml</param-value> </context-param> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> |
然后写dao层--就是数据库的实体类。
public class User { private Long id;
private String name;
private String password;
private String sex;
private Integer age; ....省略get和set方法 } |
测试,是否已经连接上数据库
package com.yangpei.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yangpei.dao.User; import com.yangpei.mapper.UserMapper;
public class DaoTest { private static ApplicationContext context=new ClassPathXmlApplicationContext("spring-context.xml"); public static void main(String[] args) { UserMapper userMapper = (UserMapper) context.getBean(UserMapper.class); System.out.println(userMapper); User user = userMapper.selectByPrimaryKey(1L); System.out.println(user.getPassword()); } } 我这里打印了数据库的密码, |