springmvc,mybatis3简单配置实例

现在很多面试都要求会springmvc, mybatis,这种架构。

这种架构有很多有点:

1:springmvc开发效率高于struts2,

2:Spring3 MVC的灵活相当的好。但是这也是他的一个缺点,太灵活的东西。使用起来可能没有strus2那样得心应手。不过spring3mvc扩展性强于struts2.

3:mybatis是一种粗粒度的框架结构。需要手写sql语句,但是把结果集封装成对象,在xml文件中进行配置,基于xml文件的动态SQL。相较于hibernate,它比较容易学习。

总的来说这个框架的配置:走的是粗粒度,灵活性,扩展性的路子。今天在这里只是简单的配置:和简单的crud的操作:希望给大家有所帮助。

工程的目录结构如下:

使用的jar包:

首先配置mybatis的使用:

在src目录下面添加sqlmap-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"""><configuration><mappers><mapper resource="com/henu/strawhat/dao/User.xml"/></mappers></configuration>

我使用的基于xml形式的配置。有人也使用注解的形式。两种都可以,看个人爱好!不过我推荐使用xml

上面代码中resource映射每一个大模块的数据操作xml.

在dao目录下面我新建了一个user.xml.所有的数据操作都在这个xml文件中。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ""><mapper namespace="com.henu.strawhat.dao.UserDao"><select id="login" parameterType="String" resultType="com.henu.strawhat.entity.User">SELECTid, userName, password, createDateFROM userWHERE userName=#{userName}</select><select id="list" resultType="com.henu.strawhat.entity.User">SELECTid, userName, password, createDateFrom user</select><insert id="add" parameterType="com.henu.strawhat.entity.User">INSERT INTOuser(userName, password,createDate)VALUES(#{userName}, #{password},now())</insert><delete id="delete" parameterType="int">DELETE FROMuserWHEREid = #{id}</delete><update id="update" parameterType="com.henu.strawhat.entity.User">UPDATEuserSETuserName=#{userName}, password=#{password}WHEREid=#{id}</update></mapper>

上面的代码没有做解释,但是相信大家都看的懂吧!

然后我架设springmvc的框架:

在xml文件中:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value> </context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter><filter-name>characterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf8</param-value></init-param><!– <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value></init-param> –></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value></param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> </web-app>

这个配置相信大家会很熟悉。大家可以在spring的官方文档中找到这样的配置;

接下来配置applicationContext.xml。在src目录下面:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi="" xmlns:context=""xmlns:aop="" xmlns:tx=""xmlns:mvc=""xsi:schemaLocation=""><context:annotation-config /><context:property-placeholder location="classpath:jdbc.properties" /><context:component-scan base-package="com.henu.strawhat" /><mvc:annotation-driven /><bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"destroy-method="close"><property name="driverClass" value="${database.driver}" /><property name="jdbcUrl" value="${database.url}" /><property name="username" value="${database.username}" /><property name="password" value="${database.password}" /><property name="idleConnectionTestPeriod" value="60" /><property name="idleMaxAge" value="240" /><property name="maxConnectionsPerPartition" value="4" /><property name="minConnectionsPerPartition" value="2" /><property name="partitionCount" value="3" /><property name="acquireIncrement" value="2" /><property name="statementsCacheSize" value="50" /><property name="releaseHelperThreads" value="3" /><property name="connectionTestStatement"><value>select * from test</value></property></bean><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/" /><property name="suffix" value=".jsp" /></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:/sqlmap-config.xml"></property><property name="dataSource" ref="dataSource" /></bean><!– define PlatformTransactionManager (declarative transaction) –><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!– ensure that the above transactional advice runs for any executionof an operation defined by the FooService interface –> <aop:config><aop:pointcut expression="execution(* com.henu.strawhat.service.impl.*.*(..))" id="myPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config><!– config the transactionnal advice –> <tx:advice id="txAdvice" transaction-manager="transactionManager"><!– the transactional semantics… –><tx:attributes><!– all methods starting with ‘get’ are read-only –><tx:method name="*" read-only="true"/><!– other methods use the default transaction settings (see below) –><tx:method name="add*" propagation="REQUIRED"/><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="reset*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="*Cookie" propagation="REQUIRED"/><tx:method name="dirCreate" propagation="REQUIRED"/></tx:attributes></tx:advice></beans>

我使用的是boncp的数据源。spring aop的方式做事务。

在这个xml文件中我引入了前面建立的mybatis的sqlmap-config.xml

我在项目中使用的注解的形式对每一层类的调用。

你可以这样理解 impossible(不可能)–I'm possible (我是可能的)。

springmvc,mybatis3简单配置实例

相关文章:

你感兴趣的文章:

标签云: