SSM+Druid的搭建

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.0modelVersion>

<groupId>com.yangpeigroupId>

<artifactId>RedLemonartifactId>

<version>0.0.1-SNAPSHOTversion>

<packaging>warpackaging>

 

<name>RedLemonname>

<url>https://home.cnblogs.com/u/InterfaceAOP/url>

<inceptionYear>2019-2020inceptionYear>

 

<properties>

<spring-version>4.1.9.RELEASEspring-version>

<mybatis.version>3.2.8mybatis.version>

<mybatis-spring.version>1.2.3mybatis-spring.version>

<druid.version>1.1.10druid.version>

<tomcat.version>2.2tomcat.version>

<webserver.port>8181webserver.port>

<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>

properties>

 

<dependencies>

<dependency>

<groupId>com.alibabagroupId>

<artifactId>druidartifactId>

<version>${druid.version}version>

dependency>

 

<dependency>

<groupId>mysqlgroupId>

<artifactId>mysql-connector-javaartifactId>

<version>5.1.42version>

dependency>

 

<dependency>

<groupId>org.mybatisgroupId>

<artifactId>mybatisartifactId>

<version>3.4.4version>

dependency>

 

<dependency>

<groupId>org.mybatisgroupId>

<artifactId>mybatis-springartifactId>

<version>1.3.1version>

dependency>

 

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-contextartifactId>

<version>${spring-version}version>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-webmvcartifactId>

<version>${spring-version}version>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-beansartifactId>

<version>${spring-version}version>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-ormartifactId>

<version>${spring-version}version>

dependency>

 

<dependency>

<groupId>org.slf4jgroupId>

<artifactId>slf4j-apiartifactId>

<version>1.6.6version>

dependency>

<dependency>

<groupId>org.slf4jgroupId>

<artifactId>slf4j-log4j12artifactId>

<version>1.6.6version>

dependency>

<dependency>

<groupId>log4jgroupId>

<artifactId>log4jartifactId>

<version>1.2.16version>

dependency>

 

dependencies>

 

<developers>

developers>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.pluginsgroupId>

<artifactId>maven-compiler-pluginartifactId>

<version>3.1version>

<configuration>

<source>1.8source>

<target>1.8target>

configuration>

plugin>

<plugin>

<groupId>org.apache.tomcat.mavengroupId>

<artifactId>tomcat7-maven-pluginartifactId>

<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"/>

 

<setting name="multipleResultSetsEnabled" value="true"/>

 

<setting name="useColumnLabel" value="true"/>

 

<setting name="useGeneratedKeys" value="false"/>

 

  

<setting name="autoMappingBehavior" value="PARTIAL"/>

 

<setting name="defaultExecutorType" value="SIMPLE"/>

 

<setting name="mapUnderscoreToCamelCase" value="true"/>

 

        <setting name="localCacheScope" value="SESSION"/>

 

        

        <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: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}" />

<property name="maxActive" value="${druid.maxActive}" />

<property name="initialSize" value="${druid.initialSize}" />

<property name="maxWait" value="${druid.maxWait}" />

<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}" />

 

<property name="poolPreparedStatements" value="${druid.poolPreparedStatements}" />

<property name="maxPoolPreparedStatementPerConnectionSize"

value="${druid.maxPoolPreparedStatementPerConnectionSize}" />

<property name="filters" value="${druid.filters}" />

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>

 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<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">

 

    

    <mvc:annotation-driven/>

  

 

    <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/**"/> 

     

    

    

   

    

    

  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

    <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.ContextLoaderListenerlistener-class>

  listener>

  <context-param>

    <param-name>contextConfigLocationparam-name>

    <param-value>classpath:spring-context.xmlparam-value>

  context-param>

  <servlet>

    <servlet-name>springServletservlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

    <init-param>

      <param-name>contextConfigLocationparam-name>

      <param-value>classpath:spring-servlet.xmlparam-value>

    init-param>

    <load-on-startup>1load-on-startup>

  servlet>

  <servlet-mapping>

    <servlet-name>springServletservlet-name>

    <url-pattern>*.dourl-pattern>

  servlet-mapping>

  <filter>

    <filter-name>encodingFilterfilter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>

    <init-param>

      <param-name>encodingparam-name>

      <param-value>UTF-8param-value>

    init-param>

    <init-param>

      <param-name>forceEncodingparam-name>

      <param-value>trueparam-value>

    init-param>

  filter>

  <filter-mapping>

    <filter-name>encodingFilterfilter-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;

....省略getset方法

}

测试,是否已经连接上数据库

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());

}

}

我这里打印了数据库的密码,

 

你可能感兴趣的:(SSM+Druid的搭建)