mybatis

<img src="https://img-blog.csdn.net/20160917213130117?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
package daisy.com;

import java.util.List;

import com.sun.javafx.collections.MappingChange.Map;

public interface OlapService {
    public int countAll();
    public void insertStudent(Student student);
    public List<Student> getAllStudent();
    public Student getById(int id);
    public void deleteStudent(int id);
    public void updateStudent(Map<String,Object> map);
}
</pre><pre name="code" class="java"><pre name="code" class="java">package daisy.com;

import java.util.List;

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

import com.sun.javafx.collections.MappingChange.Map;
@Service("olapService")
public class OlapServiceImpl implements OlapService{
	@Autowired
	StudentDao studentDao;
	public int countAll() {
		return studentDao.countAll();
	}

	public void insertStudent(Student student) {
		studentDao.insertStudent(student);
	}

	public List<Student> getAllStudent() {
		return studentDao.getAllStudent();
	}

	public Student getById(int id) {
		return studentDao.getById(id);
	}

	public void deleteStudent(int id) {

		studentDao.deleteStudent(id);
	}

	public void updateStudent(Map<String, Object> map) {
		studentDao.updateStudent(map);
	}

}


 
</pre><pre name="code" class="java">package daisy.com;


import java.util.List;


import com.sun.javafx.collections.MappingChange.Map;


public interface StudentDao {
    public int countAll();
    public void insertStudent(Student student);
    public List<Student> getAllStudent();
    public Student getById(int id);
    public void deleteStudent(int id);
    public void updateStudent(Map<String,Object> map);
}
</pre><pre name="code" class="java">

package daisy.com;

import java.util.List;

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

import com.sun.javafx.collections.MappingChange.Map;
@Service("studentDao")
public class StudentDaoImpl implements StudentDao{
	@Autowired
	private StudentMapper studentMapper;
	
	public int countAll() {
		return studentMapper.countAll();
	}

	public void insertStudent(Student student) {
		studentMapper.insertStudent(student);
	}

	public List<Student> getAllStudent() {
		return studentMapper.getAllStudent();
	}

	public Student getById(int id) {
		return studentMapper.getById(id);
	}

	public void deleteStudent(int id) {
		studentMapper.deleteStudent(id);
	}

	public void updateStudent(Map<String, Object> map) {
		studentMapper.updateStudent(map);
	}

}

package daisy.com;

import java.util.List;

import org.springframework.stereotype.Service;

import com.sun.javafx.collections.MappingChange.Map;

@Service("studentMapper")
public interface StudentMapper {
    public int countAll();
    public void insertStudent(Student student);
    public List<Student> getAllStudent();<pre name="code" class="java">package daisy.com;

public class Student {
	private String name;
	private int id;
	private int classId;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getClassId() {
		return classId;
	}
	public void setClassId(int classId) {
		this.classId = classId;
	}
	

}

public Student getById(int id); public void deleteStudent(int id); public void updateStudent(Map<String,Object> map);}

 

配置文件

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	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/context  
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     
     http://www.springframework.org/schema/mvc  
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


	<!-- 加载配置文件 -->
	<context:property-placeholder location="config.properties" />
	<!-- 指定spring注解注入层 -->
	<context:component-scan base-package="daisy.com" />
	<!-- 数据库连接池管理 -->
	<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${db.driverClass}"></property>
		<property name="jdbcUrl" value="${db.jdbcUrl}"></property>
		<property name="user" value="${db.user}"></property>
		<property name="password" value="${db.password}"></property>


		<property name="initialPoolSize" value="${db.initialPoolSize}"></property>

		<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${db.maxIdleTime}"></property>
		<!--连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${db.maxPoolSize}"></property>
		<property name="minPoolSize" value="${db.minPoolSize}"></property>

		<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${db.acquireIncrement}"></property>
		<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
		<property name="acquireRetryDelay" value="${db.acquireRetryDelay}"></property>
		<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
		<property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}"></property>
		<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 
			获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
		<property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}"></property>
	</bean>

	<!-- ================================事务相关控制================================================= -->

	<bean name="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="c3p0DataSource"></property>
	</bean>

	<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="delete*" propagation="REQUIRED" read-only="false"
				rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException" />
			<tx:method name="insert*" propagation="REQUIRED" read-only="false"
				rollback-for="java.lang.RuntimeException" />
			<tx:method name="update*" propagation="REQUIRED" read-only="false"
				rollback-for="java.lang.Exception" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="select*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut id="pc"
			expression="execution(public * daisy.com.*.*(..))" /> <!--把事务控制在Service层 -->
		<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
	</aop:config>


	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="c3p0DataSource" />
		<property name="configLocation" value="MyBatis-Configuration.xml" />
	</bean>


	<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="daisy.com.StudentMapper" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>



</beans>

db.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver 
db.jdbcUrl=jdbc:sqlserver://localhost:1433;DatabaseName=proceduretest 
db.user=sa
db.password=1


db.initialPoolSize=20 
db.maxIdleTime=60 
db.maxPoolSize=200 
db.minPoolSize=50 


db.acquireIncrement=3 
db.acquireRetryDelay=1000 
db.acquireRetryAttempts=30 
db.breakAfterAcquireFailure=false

<?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 alias="user" type="daisy.com.Student"/>  
    </typeAliases>  
      
    <!-- 指定映射文件 -->  
    <mappers>  
        <mapper resource="StudentDaoMapper.xml" />  
    </mappers>  
    
    
    
</configuration>  

<?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="daisy.com.StudentMapper">

  <parameterMap type="daisy.com.Student" id="parameterMapStudent">
    <parameter property="id"/>
    <parameter property="name"/>
    <parameter property="classId"/>
  </parameterMap>
  
  <resultMap type="daisy.com.Student" id="resultMapStudent">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="classId" column="classId"/>
  </resultMap>
  
  <insert id="insertStudent" parameterMap="parameterMapStudent">
    INSERT INTO Student(id,name,classId)
    VALUES(?,?,?)
  </insert>
  <select id="countAll" resultType="int">
        select count(*) c  from Student;
  </select>
  <select id="getAllStudent"  resultMap="resultMapStudent">
    SELECT * FROM Student
  </select>
  <select id="getById" parameterType="int" resultMap="resultMapStudent">
    SELECT * FROM Student
    WHERE id=#{value}
  </select>
  <delete id="deleteStudent" parameterType="int">
    DELETE FROM Student 
    WHERE id=#{value}
  </delete>
  <update id="updateStudent" parameterType="java.util.Map">
    UPDATE Student
    SET name=#{name}
    WHERE id=#{id}
  </update>
</mapper>




package daisy.com;

import java.util.List;

import com.sun.javafx.collections.MappingChange.Map;

public interface OlapService {
    public int countAll();
    public void insertStudent(Student student);
    public List
   
   
    
     getAllStudent();
    public Student getById(int id);
    public void deleteStudent(int id);
    public void updateStudent(Map
    
    
     
      map);
}
empty
    
    
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值