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
- SpringMVC+MyBatis自动生成Dao接口
- MyBatis自动生成实体、DAO接口、Mapping映射文件
- Mybatis 自动从数据库生成entity,mapping,dao接口
- SpringMVC之Web-Mybatis自动生成实体类,dao,mapper使用(四)
- 利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件
- Mybatis自动生成实体类,映射文件,以及dao层接口。
- mybatis自动生成mapper+pojo(mapper接口和普通dao)
- MyBatis自动生成实体类、DAO接口和Mapping映射文件的代码(逆向工程)
- mybatis自动生成mapper,dao,映射文件
- mybatis自动生成mapper,dao,映射文件
- mybatis-generator自动生成DAO文件
- MyBatis Generator自动生成Dto、Dao、Mapping
- MyBatis Generator 自动生成 Dto、Dao、Mapping
- Mybatis 自动生成dao及映射文件
- mybatis-generator自动生成dao、mapping、model
- MyBatis自动生成Entity、Dao、Mapping
- mybatis自动生成dao、Mapping文件
- mybatis,自动生成domain、dao、mapping文件
- setEnabled() vs setClickable(), what is the difference?
- Java与Highcharts实例(三) - Hello Highcharts (后台Java传递数据)
- 苹果要用Swift语言颠覆传统编程
- POJ 3667 Hotel(线段树)
- Android控件(一)下拉刷新:SwipeRefreshLayout
- SpringMVC+MyBatis自动生成Dao接口
- 详解Qtopia 2.2.0 配置和交叉编译
- mac配置jenkins
- 在windows7环境下安装tomcat7.0的详细步骤
- Dblink 实现数据库表同步
- jni常用函数
- linux centos7.0装搜狗拼音 sougou
- Android学习笔记 初识
- 蚌埠婚纱照