SSM 搭建

1.mybatis框架的搭建。

引入SSM整合所用的jar包

aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
commons-dbcp-1.4.jar
commons-logging-1.2.jar
commons-pool-1.6.jar
fastjson-1.2.13-sources.jar
fastjson-1.2.13.jar
log4j-1.2.17.jar
mybatis-3.2.2.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.0-bin.jar
spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-jdbc-3.2.13.RELEASE.jar
spring-tx-3.2.13.RELEASE.jar
spring-web-3.2.16.RELEASE.jar
spring-webmvc-3.2.16.RELEASE.jar

设置配置文件

  引入 log4j.properties

  配置文件名 MybatisConfig.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 type="com.bdqn.entity.NewDetail" alias="b"/>
      </typeAliases>

      <!-- 配置映射文件 -->
      <mappers>
      <mapper resource="com/bdqn/dao/NewMapper.xml"/>
      </mappers>
      </configuration>

dao层的映射文件

  NewMapper.xml
  NewMapper.java

  NewMapper.java内部配置

      public interface NewMapper {
      public List<NewDetail> list();
      public int update(NewDetail detail);
      public int del(@Param("a")String b);
      public int insert(Map<String,Object> obj);
      }

  NewMapper.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="com.bdqn.dao.NewMapper"> //这个地址要和NewMapper的名字相同
       //查询<select id="count" resultType="int">
      select count(id) from news_detail
      </select>
       //查询<select id="list" resultType="b">
      select * from news_detail
      </select>
       //修改<update id="update" parameterType="b">
      update news_detail set title=#{title} where id = #{id}
      </update>
      //删除<delete id="del"> 
      delete from news_detail where id=#{a}
      </delete>

       //添加<insert id="insert" parameterType="Map">
      insert into news_detail(title,author) values(#{t},#{a})
      </insert>
      </mapper>

测试Mybatis

Spring整合Mybatis

添加SpringMybatis配置文件SpringMybatis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


<!-- 配备spring数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/kgcnews?
useUnicode=true&amp;characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>

<!-- spring 接管 SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 引用数据源组件 -->
<property name="dataSource" ref="dataSource" /> //ref="dataSource"就是上面spring的数据源
<!-- 引用MyBatis配置文件中的配置 -->
<property name="configLocation" value="classpath:MybatisConfig.xml" />
</bean>

<!-- 注入给对应的值 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bdqn.dao" />//注入对应的包中
</bean>
<context:component-scan base-package="com.bdqn" />//扫描com.bdqn包
</beans>

加入springMVC

配置SpringMVC的配置文件SpringMVC.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

扫描

<context:component-scan base-package="com.bdqn.controller"></context:component-scan>

<!-- 视图解析器 作用:把逻辑视图加上前后缀生成实际的物理路径-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven/><!-- //扫描的驱动 -->

<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>

web中配置依赖

//解决中文乱码问题

<filter>
<filter-name>filterinto</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>filterinto</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

监听器

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

监听springmybatis文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMybatis.xml</param-value>
</context-param>

//SpringMVC.xml文件的加载

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMVC.xml</param-value> //springmvc 的xml文件名
</init-param>

 

转载于:https://www.cnblogs.com/qhantime/p/10752826.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值