1.项目目的
a.通过Spring+Mybatis实现通过web访问达成mySql的操作
b.理解Spring+Mybatis的配置
c.理解maven对项目的管理
2.配置文件
a.采用maven约定结构,src/main/java、src/main/resources、src/main/test
b.基本springMVC+Mybatis需要用到的jar
- spring-web、spring-webmvc、spring-core、spring-beans、spring-context、spring-context-support
- spring-jdbc、spring-tx、spring-aop、spring-asm、spring-expression、spring-test
- mybatis-3.1、mybatis-spring-1.1
- commons-logging-1.1、commons-pool1.5.4
- xalan-2.6.0(首先它是一个Java的开源类库, 它是一个XSLT的处理包,用于将XML文档转为HTML文档,文本文件TXT,或其他格式的XML文档,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter引用该jar)
- mysql-connector-java-5.1.27
c.从web.xml开始,引入springMVC配置文件(WEB-INF/mvc-config.xml、classpath:spring/application-config.xml)
d.从application-config.xml开始,引入Mybatis配置文件(classpath:mybatis-config.xml、mysql.properties)
e.从mybatis-config.xml开始,引入Mybatis的mapper.xml配置文件
3.项目实现
a.定义springMVC第一件事情需要声明DispatchServlet,然后对访问路径进行截取
b.定义Controller,需要充分利用注解,分为两类,一个是通过注解自动装配,一个是通过注解进行访问控制标志
c.定义Mybaits的数据库操作类接口+mapper.xml对应结构,通过spring和mybatis结合插件实现桥接
4.项目部署
a.maven正确配置打包插件
b.可先通过jetty:run进行运行调试
5.遇到问题处理
a.首先增加maven中jetty插件时遇到盲区,(resources资源、maven-compiler-plugin编译插件、maven-war-plugin插件、jetty-maven-plugin插件、exec-maven-plugin插件、maven-surefire-plugin测试是否忽略失败插件)
b.spring配置时候需要注意ref和value的区别,引用dataSource的时候报错了,查了半天(spring配置需要熟练)
c.springMVC对通过注解的自动装配需要进行配置的,指向顶级包名(理解spring3以后的自动装配的配置,Bean的注解)
d.打包的时候,如果需要把java目录中的xml文件打包需要定义resources,既然定义了resources就要多定义点,否则真实resources目录下的xml不会被打包出去
e.jetty不能正确启动,通常是项目部署存在问题,用maven进行项目部署通常是报依赖少了,或者依赖的包下载过程出现异常导致。
附件:
- pom.xml
<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>org.springframework.samples.service.service</groupId> <artifactId>spyu-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <!-- Generic properties --> <java.version>1.6</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Web --> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>2.5</servlet.version> <!-- Spring --> <spring-framework.version>3.1.1.RELEASE</spring-framework.version> <!-- Logging --> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> <!-- Test --> <junit.version>4.11</junit.version> <jettyVersion>8.1.8.v20121106</jettyVersion> </properties> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Spring and Transactions --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Spring jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Other Web dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.0.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.0.5</version> <scope>runtime</scope> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.4</version> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.27</version> </dependency> <dependency> <groupId>xalan</groupId> <artifactId>xalan</artifactId> <version>2.6.0</version> </dependency> <!-- Test Artifacts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> <exclude>**/.svn/*</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.java</exclude> <exclude>**/.svn/*</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <warName>spyumybatis</warName> </configuration> </plugin> <!--jetty plugin --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jettyVersion}</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/spyumybatis</contextPath> </webApp> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>9093</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass></mainClass> </configuration> </plugin> <!-- 测试出错不影响项目的编译 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> </project>
- web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spyu-mybatis</display-name> <!-- DispatcherServlet 默认加载的bean文件是/WEB-INF/(servlet-name)-servlet.xml 可以通过配置contextConfigLocation来改变加载的文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application-config.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- - Servlet that dispatches request to registered handlers (Controller implementations). --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
- mvc-config.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven/> <!-- 对org.simple下所有包下的类的注解进行扫描,并自动创建bean实例和装配bean --> <context:component-scan base-package="spyu" /> <!-- 配置视图 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> <!-- 可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView </value> </property> </bean> </beans>
- application-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" default-autowire="byName" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" > <context:annotation-config /> <!-- 载入MySql配置 文件 --> <context:property-placeholder location="classpath:Mysql.properties" /> <!-- 配置DataSource数据源 --> <bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- Spring-Mybatis整合 --> <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:MyBatis-config.xml" /> <property name="dataSource" ref="DataSource" /> </bean> <!-- 自动扫描mappar --> <bean id="autoMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="spyu.dao" /> </bean> </beans>
- 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> <typeAliases> <typeAlias alias="User" type="spyu.mode.User" /> </typeAliases> <mappers> <mapper resource="spyu/dao/mapper/UserMapper.xml"/> </mappers> </configuration>
- mysql.properties
<?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> <typeAliases> <typeAlias alias="User" type="spyu.mode.User" /> </typeAliases> <mappers> <mapper resource="spyu/dao/mapper/UserMapper.xml"/> </mappers> </configuration>
- 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"> <mapper namespace="spyu.dao.IUser"> <select id="insertUser" parameterType="User"> insert into user(name) values (#{name}) </select> </mapper>
Spring+mybatis测试项目总结,布布扣,bubuko.com