SpringMVC+MyBatis自动生成Dao接口

来源:互联网 发布:淘宝店铺如何删除宝贝 编辑:程序博客网 时间:2024/05/16 15:48

实体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 mapperPUBLIC "-//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 byid 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 whereusername = #{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;@Repositorypublic class UserMapDao {@Autowiredprivate 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://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
原创粉丝点击