SM整合-方式一

package com.entity;

public class Customer {

	private int cutNo;
	private String cutName;
	private int cutAge;
	public int getCutNo() {
		return cutNo;
	}
	public void setCutNo(int cutNo) {
		this.cutNo = cutNo;
	}
	public String getCutName() {
		return cutName;
	}
	public void setCutName(String cutName) {
		this.cutName = cutName;
	}
	public int getCutAge() {
		return cutAge;
	}
	public void setCutAge(int cutAge) {
		this.cutAge = cutAge;
	}
	
	

}
<?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>
 <!-- 数据库信息 
 	加载映射文件CustomerMapper.xml
 -->
	<mappers>
		<mapper resource="com/mapper/CustomerMapper.xml"/>
	</mappers>
</configuration>
<?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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans.xsd
    					http://www.springframework.org/schema/mvc
    					http://www.springframework.org/schema/mvc/spring-mvc.xsd
    					http://www.springframework.org/schema/context
    					http://www.springframework.org/schema/context/spring-context.xsd">
     <bean id="config" class=" org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="Locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>
     
	<bean id="customerMapper"  class="com.Dao.impl.CustomerMapperImpl">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
	</bean>
	<bean id="customerService" class="com.service.impl.CustomerServiceimpl">
		<property name="customerMapper" ref="customerMapper"></property>
	</bean>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<property name="maxActive" value="${maxActive}"></property>
		<!-- 最大空闲时间 -->
		<property name="maxIdle"   value="${maxIdle}"></property>
	</bean>
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:conf.xml"></property>
	</bean>
	
</beans>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm
username=root
password=lmt568899
maxActive=500
maxIdle=1000
package com.Dao.impl; 

import org.apache.ibatis.session.SqlSession; 
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.entity.Customer;
import com.mapper.CustomerMapper;

public class CustomerMapperImpl extends SqlSessionDaoSupport implements CustomerMapper{

	@Override
	public void addCustomer(Customer customer){
		SqlSession session = super.getSqlSession();
		// 和 conf.xml一样
		CustomerMapper cutDao = session.getMapper(CustomerMapper.class);
		cutDao.addCustomer(customer);
	}

}
package com.mapper;

import com.entity.Customer;

public interface CustomerMapper {
	public void addCustomer(Customer customer);
}
<?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.mapper.CustomerMapper">
	<select id="queryCustomerBycutNo" parameterType="int" resultType="com.entity.Customer">
		select * from Customer where cutNo=#{cutNo}	
	</select>
	<insert id="addCustomer" parameterType="com.entity.Customer">
		insert into Customer(cutNo,cutName,cutAge) values(#{cutNo},#{cutName},#{cutAge})
	</insert>
</mapper>
package com.service;

import com.entity.Customer;

public interface CustomerService {
	public void addCustomer(Customer customer);
}
package com.service.impl;

import com.entity.Customer;
import com.mapper.CustomerMapper;
import com.service.CustomerService;

public class CustomerServiceimpl implements CustomerService{

	private CustomerMapper customerMapper;
	public void setCustomerMapper(CustomerMapper customerMapper) {
		this.customerMapper = customerMapper;
	}

	@Override
	public void addCustomer(Customer customer) {
		customerMapper.addCustomer(customer);
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值