当前位置: 首页 > 编程日记 > 正文

ssm框架mysql配置_ssm框架使用详解配置两个数据源

学习ssm框架已经快一年了,今天把这个框架总结一下。

SSM 就是指 spring、SpringMVC和Mybatis。先说一下基本概念(百度上搜的)

1、基本概念

1.1、Spring

Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

1.2、SpringMVC

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

1.3、MyBatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java

Objects,普通的 Java对象)映射成数据库中的记录。

2、开发环境

开发环境需要搭建maven,这里就不介绍了

3、新建一个maven项目

File--->New--->Maven Project

2dd38fbbd0f4c3a53869f11b71d50a78.png

看到如下界面,点next

7c3895e5fdf472fbb7b011f41844e50a.png

选择最下面的1.0,然后next

03488ffc519c4811114755497ae6ad0d.png

然后写好Group id 和 Artifact id,点击finish

e6eabfe8d1f473c2707b86465e1297b1.png

这样,一个maven项目就建好了。

注意:刚建好的项目是不完整的,所以还要进行如下操作

abdf6d27c92efc8953fd30e7c0a8f360.png

右键项目--->Properties

ce9d9ef196b9068388fa765ebd78340d.png

Others改为UTF-8

Java Build Path--->Libraries,选中1.5后点击Edit..

68baa4a7ad0e3a5d0153b7a028898b2e.png

选择jdk1.8

5c9e1e62ecaa0b7574352b21aeff7ecc.png

最后项目如果是这样那就说明修改完成了

ddb2df1d04f011f746904d539b39e853.png

4、SSM整合

4.1 修改web.xml文件

be618e67002d6d925fad9f55206b8c1e.png

文件配置如下

Archetype Created Web Application

contextConfigLocation

classpath:spring-mybatis.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

true

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

default

/resources/*

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

true

FileUploadServlet

FileUploadServlet

am.controller.FileUploadServlet

SpringMVC

/

FileUploadServlet

/FileUploadServlet

/index.jsp

15

4.2 配置jdbc

将数据库配置(jdbc.properties),日志配置(log4j.properties),spring-mvc配置(spring-mvc.xml),mybatis配置(spring-mybatis.xml)全部放在resources下面

jdbc配置如下:

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://112.74.51.37:3306/am?useUnicode=true&characterEncoding=utf-8&useSSL=false

username=root

password=tq2016

#定义初始连接数

initialSize=0

#定义最大连接数

maxActive=20

#定义最大空闲

maxIdle=20

#定义最小空闲

minIdle=1

#定义最长等待时间

maxWait=60000

4.3 配置log4j

#定义LOG输出级别

log4j.rootLogger=INFO,Console,File

#定义日志输出目的地为控制台

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

#可以灵活地指定日志输出格式,下面一行是指定具体的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#文件大小到达指定尺寸的时候产生一个新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

#指定输出目录

log4j.appender.File.File = logs/ssm.log

#定义文件最大大小

log4j.appender.File.MaxFileSize = 10MB

# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志

log4j.appender.File.Threshold = DEBUG

log4j.appender.File.layout = org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

4.4 配置spring-mvc

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.1.xsd">

text/html;charset=UTF-8

4.5 配置spring-mybatis

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

4.6 pom.xml配置

pom文件中引入dependency以后可以自动下载相关jar包

4.0.0

hobby

ssm

war

0.0.1-SNAPSHOT

ssm Maven Webapp

http://maven.apache.org

4.0.2.RELEASE

3.1.1

1.7.7

1.2.17

org.apache.poi

poi

3.15

com.belerweb

pinyin4j

2.5.0

dom4j

dom4j

1.6.1

commons-httpclient

commons-httpclient

3.1

junit

junit

4.11

test

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-oxm

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.1.0

javax

javaee-api

7.0

mysql

mysql-connector-java

5.1.30

commons-dbcp

commons-dbcp

1.2.2

jstl

jstl

1.2

log4j

log4j

${log4j.version}

com.alibaba

fastjson

1.1.41

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

org.codehaus.jackson

jackson-mapper-asl

1.9.13

org.apache.httpcomponents

httpclient

4.5.2

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

commons-codec

commons-codec

1.9

org.apache.commons

commons-email

1.4

com.fasterxml.jackson.core

jackson-annotations

2.8.1

com.fasterxml.jackson.core

jackson-core

2.8.1

com.fasterxml.jackson.core

jackson-databind

2.8.1

javax.servlet

javax.servlet-api

3.1.0

provided

com.google.code.gson

gson

2.6.2

ssm

需要注意的是:必须在main/java下面加这四个包,如果不加的话就把spring-mvc.xml和spring-mybatis.xml文件中的相关部分注释掉,否则会报错。

需加如下四个包

8fe6be03736af0302570c3c029c82dfd.png

如果不加需要注释掉spring-mvc.xml中

d8c710b2fc9633fc473e6cd711bf4170.png

和spring-mybatis.xml中

fa7db90cf19584c3f5d7b04446fa83ee.png

这样就OK了

5、接下来开始配置两个数据源

5.1 将jdbc和mybatis文件各复制一份

0a33a1572d7e26398c517097bfa86423.png

jdbc就不多说了,和原版一样,换一下数据库就好了,如下

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/zizai?useUnicode=true&characterEncoding=utf-8

username=root

password=root

#aimo数据库连接

#url=jdbc:mysql://112.74.51.37:3306/back?useUnicode=true&characterEncoding=utf-8

#username=root

#password=tq2017

#定义初始连接数

initialSize=0

#定义最大连接数

maxActive=20

#定义最大空闲

maxIdle=20

#定义最小空闲

minIdle=1

#定义最长等待时间

maxWait=60000

5.2 配置spring-mybatis-back.xml

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

5.3 配置spring-mybatis-front.xml

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

5.4 配置web.xml

只需要在原来的基础上多引入一个配置文件即可

contextConfigLocation

classpath:spring-mybatis-front.xml,

classpath:spring-mybatis-back.xml

注意:

1、项目目录最好分开写

a339b1a92762bef6138b55e403014842.png

2、我配置好以后在项目启动时不会报错,但是调用方法时会报错

严重: Servlet.service() for servlet [SpringMVC] in context with path [/twodatasource] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'

### The error may exist in file [D:\aimospace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\twodatasource\WEB-INF\classes\com\twodatasource\front\mapping\UserMapper.xml]

### The error may involve com.twodatasource.front.dao.UserMapper.selectAll

### The error occurred while executing a query

### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'] with root cause

java.lang.ClassNotFoundException: ${driver}

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:260)

at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)

at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)

at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)

at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)

at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:80)

at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:66)

at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279)

at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:69)

at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:56)

at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267)

at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141)

at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)

at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:483)

at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355)

错误信息是加载不了{driver}

后来在配置文件中把它写死了就可以了

初始化连接大小连接池最大数量连接池最大空闲连接池最小空闲获取连接最大等待时间

关于这一点我也在网上搜索了一些答案,但是都没有能够正确的解决的。所以如果有哪位大神搞定了还请不吝啬赐教一下。

68f724bad5a33d9a9f6bd2ca7369ecb7.png

相关文章:

Linux下JDK环境的配置

whereis javawhich java (java执行路径)echo $JAVA_HOME rpm -ivh jdk-7u79-linux-x64.rpm 配置profile 转载于:https://www.cnblogs.com/xubc/p/4686748.html

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar

通过Spring的jdbcTemplate调用Mysql的存储过程,出现下面的问题(以前也使用过,并没有出现下面的问题,折腾大半天,郁闷):开始报下面的错误:[INFO ]2014-07-01 10:49:15,297 MESSAGE : …

Aras学习笔记(1)学习Aras已半年有余,也积攒一些学习笔记,今天起会陆续分享出来,有兴趣的朋友一起交流...

Aras Innovator PLM简介 美国Aras公司的产品生命周期(PLM)软件。Aras Innovator是微软在PLM领域唯一的一家金牌合作伙伴。是全球首款达到CMII 4星级的开放许可的企业级PLM(OPEN PLM)产品。通过软件许可(节点)免费,服务…

LTE CRS 时频资源

1. 参考 Spec 3GPP-36.211-6.10 Cell-specificReference Signal (CRS) Cell-specificreference signals are transmitted on one or several of antenna ports 0 to 3. Cell-specific reference signals are defined for Δf 15 kHzonly 2. 时频位置公式 在36.211-6.10.1.…

wcf客户端捕获异常

直接使用Exception进行捕获,然后在监视器中查看具体是哪一个异常 System.Exception {System.ServiceModel.Security.MessageSecurityException} 查看StackTrace Server stack trace: 在 System.ServiceModel.Channels.SecurityChannelFactory1.SecurityRequestChan…

java监听mysql_java实时监控mysql数据库变化

对于二次开发来说,很大一部分就找找文件和找数据库的变化情况对于数据库变化。还没有发现比较好用的监控数据库变化监控软件。今天,我就给大家介绍一个如何使用mysql自带的功能监控数据库变化1、打开数据库配置文件my.ini (一般在数据库安装目录)(D:MYSQ…

SAS、R以及SPSS的比较__统计语言大战

转载于:https://www.cnblogs.com/bicoffee/p/3818626.html

第九章实验报告

C程序设计实验报告 实验项目:构造数据类型实验 姓名:罗子健 实验地点:物联网实验室 实验时间:2019年6月26日 一、实验目的与要求 (1)掌握结构体类型说明和结构体类型变量、数组、指针的定义方法及使用。 …

LTE Paging时频资源

介绍PAGING的文章有很多,本文主要介绍Paging的时频资源。 为了达到省电的目的,UE在idle下使用DRX。UE在什么时刻(SFN/subframe)wakeup起来接受Paging 呢? 先介绍2个概念,如下: PO: Paging Occasion, 它是一个子帧,这…

mysql处理上百万条的数据库如何优化语句来提高处理查询效率

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t whe…

smarty mysql_Smarty处理mysql查询数组

Smarty处理mysql查询数组MySQL的查询结果一般是一个数组,而不是所有结果集。因此我们需要将结果全部存到数组中进行处理,然后就可以很轻松的再Smarty中使用了。PHP Mysql 代码$sql"select article_id,article_title from tbl_article order by arti…

Linux文件压缩与解压缩

什么是压缩文件?原理是什么?简单的说,就是经过压缩软件压缩文件叫压缩文件,压缩的原理是把文件的二进制代码压缩,把相邻的0,1代码减少,例如有000000,可以把它变成6个0的写法60来减少该文件的空间&#xff…

git 使用和一些错误

一、简单使用 Git是目前世界上最先进的分布式版本控制系统,用于自动记录每次文件的改动,但是和所有版本控制系统一样,只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等,而图片、视频这…

LTE Paging消息的接收

Paging消息的内容如下: 在idle和connected 下,UE可以分别接受如下信息: RRC 状态对应的Paging消息内容Idle1.系统消息改变 2. ETWS 3.呼叫请求Connected1.系统消息改变 2. ETWS 寻呼消息的设计初衷是: UE在idle态下&#xff…

android开发之Parcelable使用详解

想要在两个activity之间传递对象,那么这个对象必须序列化,android中序列化一个对象有两种方式,一种是实现Serializable接口,这个非常简单,只需要声明一下就可以了,不痛不痒。但是android中还有一种特有的序…

tomcat mysql 中文乱码_tomcat 中文乱码, mysql 中文乱码_MySQL

Tomcattomcat中文乱码 get 请求.修改server.xml中的添加URIEncodingUTF-8tomcat中文乱码 post 版另外HttpURLConnection上传参数的时候要转码成url编码 outStream.writeBytes("&" URLEncoder.encode(key, "utf-8") "" URLEncoder.encode(…

自己写的Python数据库连接类和sql语句拼接方法

这个工具类十分简单和简洁。 sql拼接方法 # encodingutf-8 from django.http import HttpResponse from anyjson import serialize from django.http import HttpResponse from anyjson import serialize import MySQLdbdef safe(s):return MySQLdb.escape_string(s)def get_i_…

Koa 本地搭建 HTTPS 环境

openssl 首先本地需要安装 openssl,用于生成自签名证书。 $ brew install openssl检查安装: $ openssl version LibreSSL 2.6.5生成证书 执行以下命令生成证书: openssl req -nodes -new -x509 -keyout server.key -out server.cert Generati…

eDRX中的Paging

在idle下,Legacy LTE的DRX周期最大值为2.56s, 频繁的唤醒会消耗UE的电量。为了降低功耗,延长待机时间,在Release 13,NB-IOT引入eDRX模式。eDRX就是Extended idle-mode DRX cycle,扩展不连续接受。 下面介绍超帧(Hyper-SFN)的概念…

ios 图片自动轮播

ios 图片自动轮播 #import "NYViewController.h"#define kImageCount 5interface NYViewController () <UIScrollViewDelegate> property (nonatomic, strong) UIScrollView *scrollView; property (nonatomic, strong) UIPageControl *pageControl;property (…

mysql练习题及答案_MySQL经典练习题及答案,常用SQL语句练习50题

#--插入学生表测试数据#(01 , 赵雷 , 1990-01-01 , 男)insert into Student values(01 , 赵雷 , 1990-01-01 , 男);insert into Student values(02 , 钱电 , 1990-12-21 , 男);insert into Student values(03 , 孙风 , 1990-05-20 , 男);insert into Student values(04 , 李云 …

一次因NAS存储故障引起的Linux系统恢复案例

推荐&#xff1a;10年技术力作&#xff1a;《高性能Linux服务器构建实战Ⅱ》全网发行&#xff0c;附试读章节和全书实例源码下载&#xff01;一、故障现象描述NAS操作系统内核为Linux&#xff0c;自带的存储有16块硬盘&#xff0c;总共分两组&#xff0c;每组做了RAID5&#xf…

手机网页H5 自适应不同分辨率的屏幕 必学标签meta之viewport

viewport 语法介绍 <meta name"viewport"content" height [pixel_value | device-height] , width [pixel_value | device-width ] , initial-scale float_value , minimum-scale float_value , maximum-scale float_value , user-scalable [yes | no]…

PSM-省电模式(PowerSaving Mode)

PSM: PowerSaving Mode, 省电模式, 是R12引入的新feature, spec可以参考&#xff1a;3GPP 24.301-5.3.11 Powersaving mode 和 23.682-4.5.4 UEPower Saving Mode.在PSM模式下&#xff0c;网络无法到达UE&#xff0c;UE无法接受来自于网络的数据和请求&#xff0c;类似于关机&…

mysql keepalived低版本_Mysql+keepalived主主切换

Mysqlkeepalived主主切换一&#xff0c;环境介绍网络结构&#xff1a;VIP :192.168.1.30MYSQL A:192.168.1.21MYSQL B:192.168.1.22二、mysql主主同步要实现mysqlkeepalived主主切换&#xff0c;首先要实现的就是两台mysql服务器的主主同步&#xff0c;查看http://smalldeng.bl…

Android环境搭建和Android HelloWorld—Android开发环境搭建

Android_Android开发环境搭建-搭建Android的开发环境 1.我考虑先下载JDK7.0,在JDK的安装中&#xff0c;考虑一般SDK都是向上兼容的&#xff0c;于是选择了最高的版本7.0这里是我总结的详细的JDK配置以及路径配置的过程&#xff1a; JavaSDK安装&#xff1a;安装JDK.exe然后配置…

30个在线学习设计与开发的站点

转&#xff1a;http://www.w3cschool.cc/w3cnote/30-best-websites-to-learn.html

【07月01日】A股滚动市净率PB历史新低排名

2010年01月01日 到 2019年07月01日 之间&#xff0c;滚动市净率历史新低排名。 上市三年以上的公司&#xff0c;2019年07月01日市净率在30以下的公司。 来源&#xff1a;A股滚动市净率(PB)历史新低排名。 1 - XD中国石(SH601857) - 历史新低 - PB_TTM&#xff1a;1.03 - PE_TTM…

LTE-连接态下的DRX

C-DRX: Connectedmode DRX,连接态下的DRX UE在连接态下&#xff0c;如果没有数据传输的话&#xff0c;会根据DRX的规则停止监听PDCCH(监听PDCCH可参考&#xff1a;PDCCH),从而达到省电的目的。一个DRX周期包含On Duration 和Opportunity for DRX 两个时间段。3GPP - 36.321中示…

mvc mvp mvvm的区别与联系_MVC,MVP,MVVM比较以及区别(上)

MVC&#xff0c;MVP&#xff0c;MVVM都是用来解决界面呈现和逻辑代码分离而出现的模式&#xff0c;以前只是对他们有部分的了解&#xff0c;没有深入的研究过&#xff0c;对于一些里边的概念和区别也是一知半解。现在一边查资料&#xff0c;并结合自己的理解&#xff0c;来谈一…