SM整合-方式一

xiaoxiao2025-04-28  20

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); } }

 

转载请注明原文地址: https://www.6miu.com/read-5029356.html

最新回复(0)