1.mybatis框架的搭建。
引入SSM整合所用的jar包
aopalliance-1.0.jar
aspectjweaver-1.6.9.jarcommons-dbcp-1.4.jarcommons-logging-1.2.jarcommons-pool-1.6.jarfastjson-1.2.13-sources.jarfastjson-1.2.13.jarlog4j-1.2.17.jarmybatis-3.2.2.jarmybatis-spring-1.2.0.jarmysql-connector-java-5.1.0-bin.jarspring-aop-3.2.13.RELEASE.jarspring-beans-3.2.13.RELEASE.jarspring-context-3.2.13.RELEASE.jarspring-core-3.2.13.RELEASE.jarspring-expression-3.2.13.RELEASE.jarspring-jdbc-3.2.13.RELEASE.jarspring-tx-3.2.13.RELEASE.jarspring-web-3.2.16.RELEASE.jarspring-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.javaNewMapper.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&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>