Spring+SpringMVC+MyBatis集成(SSM)
程序员文章站
2022-06-14 17:27:24
1、导入需要用到的jar包 2、数据库连接配置(jdbc.properties) 3、打印日志配置(可不要:log4j.properties【名字必须为这个】) 4、Spring+MyBatis的集成配置(applicationContext.xml) 5、SpringMVC配置(applicati ......
1、导入需要用到的jar包
<dependencies> <!--spring核心包--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <!--springmvc核心包--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <!--spring-test测试--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>${spring.version}</version> </dependency> <!--文件上传--> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.3</version> </dependency> <!--jackson--> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>2.9.8</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> </dependency> <!--servlet的包--> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.0.1</version> </dependency> <!--mybatis核心包--> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.6</version> </dependency> <!--数据库驱动包--> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.46</version> </dependency> <!--log4j日志包:用于打印详细日志(可不要):需配合log4j.properties使用--> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> <!--mybatis与spring整合包是mybatis提供的(mybatis-spring-xxxx.jar),不是spring-orm--> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis-spring</artifactid> <version>1.3.2</version> </dependency> <!--spring的支持--> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-jdbc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <!--连接池--> <dependency> <groupid>commons-dbcp</groupid> <artifactid>commons-dbcp</artifactid> <version>1.4</version> </dependency> </dependencies>
2、数据库连接配置(jdbc.properties)
jdbc.driverclassname=com.mysql.jdbc.driver jdbc.url=jdbc:mysql:///数据库名称?usessl=false&useunicode=true&characterencoding=utf8 jdbc.username=用户名 jdbc.password=密码
3、打印日志配置(可不要:log4j.properties【名字必须为这个】)
#log4j.properties(日志文件:) log4j.rootlogger=error, stdout #log4j.rootlogger=none log4j.logger.cn.wang=trace 注意:cn.wang是需要要打印日志的java文件所在的包 log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d %p [%c] - %m%n
4、spring+mybatis的集成配置(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" 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"> <!--用来整合mybatis--> <!--事务使用:扫描的包 service--> <context:component-scan base-package="cn.wang.service"/> <!--引用jdbc配置的属性文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--数据源--> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="${jdbc.driverclassname}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!--配置sqlsessionfactory--> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <property name="datasource" ref="datasource" /> <!--配置别名--> <property name="typealiasespackage" value="cn.wang.domain"/> <!--注册映射文件:扫描mapper目录下的所有mapper.xml文件--> <property name="mapperlocations" value="cn.wang.mapper.*mapper.xml"/> </bean> <!--注入映射器(给单独一个mapper接口)--> <!--<bean id="usermapper" class="org.mybatis.spring.mapper.mapperfactorybean"> <property name="mapperinterface" value="cn.wang.mapper.usermapper" /> <property name="sqlsessionfactory" ref="sqlsessionfactory" /> </bean>--> <!--注入映射器(给全部mapper接口:自动扫描mapper接口生成代理注入到spring)--> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="cn.wang.mapper" /> </bean> <!--事务管理--> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource"/> </bean> <!--以注解的形式进行事务管理--> <tx:annotation-driven transaction-manager="transactionmanager"/> </beans>
5、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: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"> <!--扫描的包controller--> <context:component-scan base-package="cn.wang.controller"/> <!--启用springmvc的注解支持--> <mvc:annotation-driven/> <!--静态资源放行--> <mvc:default-servlet-handler/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/views/"/> <property name="suffix" value=".jsp"/> </bean> <!--文件上传解析器--> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize"> <value>200000000</value> </property> </bean> </beans>
6、web.xml配置
<?xml version="1.0" encoding="utf-8"?> <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"> <!--spring核心配置文件--> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param> <!--springmvc的核心控制器--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <!--springmvc的配置文件--> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext-mvc.xml</param-value> </init-param> <!--即springmvc跟着服务器(tomcat)的启动而启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <!--使用杠(/)更加符合咱们的restful风格 --> <url-pattern>/</url-pattern> </servlet-mapping> <!--spring启动的监听器--> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- 配置相应的过滤器:角色springmvc 的post请求的乱码问题 --> <!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 --> <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> </web-app>
7、mybatis的映射文件(实体类名+mapper.xml)
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace:映射器的全限定名--> <mapper namespace="cn.wang.mapper.usermapper"> <!--id:映射器中对应的方法名,resulttype:返回的数据类型(这里使用了别名,别名在applicationcontext.xml里配置)--> <select id="findall" resulttype="user"> select * from t_user </select> </mapper>
上一篇: 模版方法设计模式(抽象类)