SSM框架整合

来源:互联网 发布:交换机端口隔离作用 编辑:程序博客网 时间:2024/06/02 03:38

SSM(Spring MVC、Mybatis、Spring)


简单说明:

1.MyController是springmvc的控制文件,相当于struts2的action和struts.xml
2.UserMapper.xml是实体类dao的映射文件,UserMapper.java是dao接口,没有实现类,直接和它的xml文件整合在一起。UserMapper.xml、UserMapper.java和实体类可以通过mybatis-generator插件直接形成。
3.IUserService接口和UserServiceImpl是服务层的接口和实现,给MyController调用
4.具体实例链接 http://download.csdn.net/detail/zzhao114/9748368,实现简单的增删查改操作 (含mybatis-generator插件及使用说明)


整体结构图:


mysql的属性配置文件:

jdbc.driver=com.mysql.jdbc.Driverjdbc.password=adminjdbc.username=rootjdbc.url=jdbc\:mysql\://localhost\:3306/ssm?useUnicode\=true&characterEncoding\=gbk


Spring和myBatis整合配置文件:

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.serviceImpl" /> <!-- 配置数据库配置文件 --> <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="location" value="classpath:mysql.properties" /> </bean> <!-- 配置数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  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}" /> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource" />  <!-- 自动扫描mapping.xml文件 -->  <property name="mapperLocations" value="classpath:com/mapping/*.xml"></property>   </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  <property name="basePackage" value="com.dao" />  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 事务通知 --> <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  <property name="dataSource" ref="dataSource" /> </bean> <bean id="user" class="com.pojo.User" scope="prototype"/></beans>



Spring-MVC配置文件:

<?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/mvc              http://www.springframework.org/schema/mvc/spring-mvc-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/context              http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描所有的 controller --> <context:component-scan base-package="com.Controllers" /> <!-- 启动注解驱动 SpringMVC 功能 --> <mvc:annotation-driven /> <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <property name="prefix" value="/" />  <property name="suffix" value=".jsp" /> </bean></beans>

实体类的映射文件:

<?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="com.dao.UserMapper"> <resultMap id="BaseResultMap" type="com.pojo.User">  <id column="id" property="id" jdbcType="INTEGER" />  <result column="username" property="username" jdbcType="VARCHAR" />  <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List">  id, username, password </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap"  parameterType="java.lang.Integer">  select  <include refid="Base_Column_List" />  from user  where id = #{id,jdbcType=INTEGER} </select> <select id="selectAll" resultMap="BaseResultMap">  select * from user </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">  delete from user  where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.pojo.User">  insert into user (id, username, password  )  values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR},  #{password,jdbcType=VARCHAR}  ) </insert> <insert id="insertSelective" parameterType="com.pojo.User">  insert into user  <trim prefix="(" suffix=")" suffixOverrides=",">   <if test="id != null">    id,   </if>   <if test="username != null">    username,   </if>   <if test="password != null">    password,   </if>  </trim>  <trim prefix="values (" suffix=")" suffixOverrides=",">   <if test="id != null">    #{id,jdbcType=INTEGER},   </if>   <if test="username != null">    #{username,jdbcType=VARCHAR},   </if>   <if test="password != null">    #{password,jdbcType=VARCHAR},   </if>  </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.pojo.User">  update user  <set>   <if test="username != null">    username = #{username,jdbcType=VARCHAR},   </if>   <if test="password != null">    password = #{password,jdbcType=VARCHAR},   </if>  </set>  where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.pojo.User">  update user  set username = #{username,jdbcType=VARCHAR},  password = #{password,jdbcType=VARCHAR}  where id = #{id,jdbcType=INTEGER} </update></mapper>


插件的generatorConfig配置

生成时候就可以把这个配置文件删除
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" ><generatorConfiguration> <classPathEntry location="E:/mysql-connector-java-5.0.0-beta-bin.jar" /> <context id="store" targetRuntime="MyBatis3">  <commentGenerator>   <!-- 是否去除自动生成的注释 true:是 : false:否 -->   <property name="suppressAllComments" value="true" />   <!-- 是否去除所有自动生成的文件的时间戳,默认为false -->   <!-- <property name="suppressDate" value="false"/> -->  </commentGenerator>  <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  <jdbcConnection driverClass="com.mysql.jdbc.Driver"   connectionURL="jdbc:mysql://localhost:3306/test" userId="root"   password="admin">  </jdbcConnection>  <javaModelGenerator targetPackage="com.model"   targetProject="Mybatis\src">   <!-- enableSubPackages:是否让schema作为包的后缀 -->   <property name="enableSubPackages" value="false" />   <!-- 从数据库返回的值被清理前后的空格 -->   <property name="trimStrings" value="true" />  </javaModelGenerator>  <!-- 配置生成相应的实体Mapper.xml,对于Mapper3.X我们需要把type="XMLMAPPER" -->  <sqlMapGenerator targetPackage="com.mapping"   targetProject="Mybatis\src">   <property name="enableSubPackages" value="false" />  </sqlMapGenerator>  <!-- 配置生成相应的接口类,对应与Mapper.xml中的一系列CRUD方法SQL语句 -->  <javaClientGenerator targetPackage="com.dao"   targetProject="Mybatis\src" type="XMLMAPPER">   <property name="enableSubPackages" value="false" />  </javaClientGenerator>  <!-- 用户表 -->  <table schema="stone" tableName="user" domainObjectName="User"   enableCountByExample="false" enableUpdateByExample="false"   enableDeleteByExample="false" enableSelectByExample="false"   selectByExampleQueryId="false">  </table>  <table schema="stone" tableName="role" domainObjectName="Role"   enableCountByExample="false" enableUpdateByExample="false"   enableDeleteByExample="false" enableSelectByExample="false"   selectByExampleQueryId="false">  </table> </context></generatorConfiguration>

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_3_1.xsd" id="WebApp_ID" version="3.1"> <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet>  <servlet-name>SpringMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:spring-mvc.xml</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> <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>





4 0
原创粉丝点击