SpringMVC+MyBatis自动生成Dao接口

实体User代码

package com.zjp.SpringMVCTest.pojo;

public class User {

	private long id;
	private String username;
	private String password;
	private String friends;
	private String devicetoken;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getFriends() {
		return friends;
	}
	public void setFriends(String friends) {
		this.friends = friends;
	}
	public String getDevicetoken() {
		return devicetoken;
	}
	public void setDevicetoken(String devicetoken) {
		this.devicetoken = devicetoken;
	}
	
}

映射文件UserMapper.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.zjp.SpringMVCTest.pojo.UserMapper">

	<resultMap type="com.zjp.SpringMVCTest.pojo.User" id="user">
		<id property="id" column="id" />
		<result property="username" column="username"></result>
		<result property="password" column="password"></result>
		<result property="friends" column="friends"></result>
		<result property="devicetoken" column="devicetoken"></result>
	</resultMap>
	<parameterMap type="com.zjp.SpringMVCTest.pojo.User" id="parameterMapuser">
		<parameter property="id" />
		<parameter property="username" />
		<parameter property="password" />
		<parameter property="friends" />
		<parameter property="devicetoken" />
	</parameterMap>
	<select id="findAllUser" resultMap="user">
		select * from user order by
		id asc
	</select>
	<select id="findUserById" resultMap="user" parameterType="Long">
		select
		* from user where id = #{typeId}
	</select>
	<insert id="insertUser" parameterMap="parameterMapuser">
		insert into user
		(username,password)
		values
		(#{username},#{password})
	</insert>
	<delete id="deleteUser" parameterType="java.lang.String">
		delete from user where
		username = #{username}
	</delete>
</mapper>
创建Dao层接口

package com.zjp.SpringMVCTest.pojo;



public interface UserMapper {

	void insertUser(User user);
}

调用接口

package com.zjp.SpringMVCTest.mapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zjp.SpringMVCTest.pojo.User;
import com.zjp.SpringMVCTest.pojo.UserMapper;

@Repository
public class UserMapDao {

	@Autowired
	private UserMapper mapper;
	
	public void save(User user){
		this.mapper.insertUser(user);
	}
}

Spring 配置applicationContext.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:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
						http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    					http://www.springframework.org/schema/mvc 
    					http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.0.xsd
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
						http://www.springframework.org/schema/tx
    					http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
	
	
	<!-- 开启文件扫描 -->
	<context:component-scan base-package="com.zjp.SpringMVCTest" />
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="location" value="classpath:jdbc.properties"></property>
	</bean>
	<!-- 配置dbcp连接池  -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.Driver}"></property>
		<property name="url" value="${jdbc.Url}"></property>
		<property name="username" value="${jdbc.User}"></property>
		<property name="password" value="${jdbc.Password}"></property>
		<!-- 关闭自动提交 开启事物管理 -->
		<property name="defaultAutoCommit" value="false"></property>
	</bean>
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-cfg.xml"></property>
	</bean>
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<strong><span style="color:#ff0000;"> <!-- ScanMapperFiles -->
  <span style="white-space:pre">	</span><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <span style="white-space:pre">		</span><property name="basePackage" value="com.zjp.SpringMVCTest.pojo" />
 <span style="white-space:pre">	</span></bean></span></strong>
	<!-- 配置事务切面Bean,指定事务管理器 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<!-- 用于配置详细的事务语义 -->
		<tx:attributes>
			<tx:method name="get*" propagation="SUPPORTS"  read-only="true"/> 
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/> 
			<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
		</tx:attributes>
	</tx:advice>
	<aop:config proxy-target-class="true">
		<!-- 配置一个切入点,匹配com.cmcc.omp.SecurityPlatform.service下所有接口以及实现类 -->
		<!-- expression中的为AspectJ语法 -->
		<aop:pointcut expression="execution(* com.zjp.SpringMVCTest.service.*.*(..))" id="serviceMethod" />
			<!-- 指定在serviceMethod切入点应用txAdvice实物切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config>
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值