Spring Web项目展MyBatis框架

如何建立Spring Web项目参考
如何建立Spring Web项目

一 首先引入依赖

<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
	</dependency>

二 修改Spring-common.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.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
		
		<context:annotation-config/>
		<context:component-scan base-package="com.st.ssm.Service"/>
		
		<!-- 引入数据源 -->
		<context:property-placeholder location="classpath:JDBC.properties"/>
	<!-- 数据源  属性名与上面的不同-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
</bean>
		
<!-- MyBatis sql -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
		
<!--扫描mapper.java -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.st.ssm.Mapper"/>
<!-- <property name="sqlsessionFactory" ref="sqlsessionFactory" />-->
</bean>	
</beans>

增加了MyBatis sql和扫描mapper

<!-- MyBatis sql -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
		
<!--扫描mapper.java -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.st.ssm.Mapper"/>
<!-- <property name="sqlsessionFactory" ref="sqlsessionFactory" />-->
</bean>

增加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>
	<settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
	<typeAliases>
		<typeAlias alias="UserModel"
			type="com.st.ssm.Model.UserModel" />
	</typeAliases>
	<mappers>
		<mapper resource="Mapper.xml" />
	</mappers>
</configuration>

建立接口 和 mapper.xml文件

面向接口开发

建立mapper层

package com.st.ssm.Mapper;

import org.apache.ibatis.annotations.Mapper;

import com.st.ssm.Model.UserModel;



@Mapper
public interface UserMapper {
    int insert(UserModel model);
}

创建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="com.st.ssm.Mapper.UserMapper">

	<!-- Result Map -->
	<resultMap id="BaseResultMap" type="UserModel">
		<result column="id" property="id" />
		<result column="usercode" property="code" />
		<result column="username" property="name" />
		<result column="password" property="pass" />
	</resultMap>

	<!-- table all fields   sql片段 -->
	<sql id="Base_Column_List">
		id, usercode, username, password
	</sql>

	<!-- 查询条件 -->
	<sql id="Example_Where_Clause">
		where 1=1
			<if test="id != null">
				and id = #{id}
			</if>
			<if test="code != null and code != ''">
				and usercode = #{code}
			</if>
			<if test="name != null and name != ''">
				and username = #{name}
			</if>
			<if test="pass != null and pass != ''">
				and password = #{pass}
			</if>
	</sql>

	<!-- 插入记录 -->
	<!--selectKey  会将 SELECT LAST_INSERT_ID()的结果放入到传入的model的主键里面,  
        keyProperty 对应的model中的主键的属性名,这里是 user 中的id,因为它跟数据库的主键对应  
        order AFTER 表示 SELECT LAST_INSERT_ID() 在insert执行之后执行,多用与自增主键,  
              BEFORE 表示 SELECT LAST_INSERT_ID() 在insert执行之前执行,这样的话就拿不到主键了,  
                    这种适合那种主键不是自增的类型  
        resultType 主键类型 -->  
<!-- 		<selectKey resultType="java.lang.Integer"  -->
<!-- 		order="AFTER" keyProperty="id"> -->
<!-- 			SELECT LAST_INSERT_ID() -->
<!-- 		</selectKey> -->
	<insert id="insert" parameterType="Object" useGeneratedKeys="true" keyProperty="id">
		insert into user(code, name, pass)
		values(#{code}, #{name}, #{pass})
	</insert>
	
	<!-- 删除记录 -->
	<delete id="delete" parameterType="Object">
		delete from st_jsp.user where id = #{id}
	</delete>
	
	<delete id="deleteModel" parameterType="Object">
		delete from st_jsp.user 
		<include refid="Example_Where_Clause" />
	</delete>

	<!-- 根据,修改记录 -->
	<update id="update" parameterType="Object">
		update st_jsp.user set
		username = #{name}, password = #{pass}
		where usercode = #{code}
	</update>

	<!-- 修改记录,只修改只不为空的字段 -->
	<update id="updateActive" parameterType="Object">
		update st_jsp.user set 
		<trim suffixOverrides=",">
			<if test="name != null and name != '' ">
				username = #{name},
			</if>
			<if test="pass != null and pass != '' ">
				password = #{pass},
			</if>
		</trim>
		where usercode = #{code}
	</update>

	<!-- 根据查询 系统用户 -->
	<select id="selectId" resultMap="BaseResultMap" parameterType="Object">
		select <include refid="Base_Column_List" />
		from st_jsp.user where usercode = #{id}
	</select>

	<!-- 系统用户 列表总数 -->
	<select id="selectCount" resultType="java.lang.Integer" 
	parameterType="UserModel">
		select count(1) from st_jsp.user
		<include refid="Example_Where_Clause" />
	</select>

	<!-- 查询系统用户列表 -->
	<select id="selectModel" resultMap="BaseResultMap" parameterType="Object">
		select <include refid="Base_Column_List" />
		from st_jsp.user
		<include refid="Example_Where_Clause" />
<!-- 		limit #{rowStart},#{pageLimit} -->
		limit ${rowStart},${pageLimit}
<!-- 		<if test="pager.orderCondition != null and pager.orderCondition != ''"> -->
<!-- 			${pager.orderCondition} -->
<!-- 		</if> -->
<!-- 		<if test="pager.mysqlQueryCondition != null and pager.mysqlQueryCondition != ''"> -->
<!-- 			${pager.mysqlQueryCondition} -->
<!-- 		</if> -->
	</select>
	
	<!-- 查询系统用户列表 -->
	<select id="selectAll" resultMap="BaseResultMap" parameterType="Object">
		select <include refid="Base_Column_List" />
		from st_jsp.user
		<include refid="Example_Where_Clause" />
<!-- 		<if test="pager.orderCondition != null and pager.orderCondition != ''"> -->
<!-- 			${pager.orderCondition} -->
<!-- 		</if> -->
	</select>

</mapper>   

猜你喜欢

转载自blog.csdn.net/NuanShuTT/article/details/108368223
今日推荐