package com.zxh.customer.mapper.entity;
public class User {
private Long custId;
private String custName;
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
}
package com.zxh.customer.mapper.model;
import java.util.List;
import java.util.Map;
import com.zxh.customer.mapper.entity.User;
public interface UserMapper {
// 方法名要和SQL映射文件中的id相同 包名+接口名与mapper的namespace相同
public User selectUserById(Long custId);
public List<User> selectUsersByName(User user);
public void saveUser(User user);
public void updateUser(User user);
public void deleteUser(Long custId);
/**
* 使用map, 失去面向对象的领域模型概念
*/
// public Map selectUserById(Map map);
//
// public List<Map> selectUsersByName(Map map);
//
// public void saveUser(Map map);
//
// public void updateUser(Map map);
//
// public void deleteUser(Map map);
}
<?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.zxh.customer.mapper.model.UserMapper">
<select id="selectUserById" parameterType="long" resultType="User">
SELECT CUST_ID as "custId", CUST_NAME "custName" FROM CUST WHERE
CUST_ID = #{id}
</select>
<!-- like #{custName} 或者 like contact(contact('%', #{custName}), '%')-->
<select id="selectUsersByName" parameterType="User" resultType="User">
SELECT CUST_ID as "custId", CUST_NAME "custName" FROM CUST WHERE
CUST_NAME like '%${custName}%'
</select>
<insert id="saveUser" parameterType="User">
insert into cust(cust_id,
cust_name) values(seq_cust.nextval, #{custName})
</insert>
<update id="updateUser" parameterType="User">
update cust set cust_name = #{custName} where cust_id = #{custId}
</update>
<delete id="deleteUser" parameterType="int">
delete from cust where
cust_id = #{custId}
</delete>
</mapper>
package com.zxh.customer.mapper.service;
import java.util.List;
import com.zxh.customer.mapper.entity.User;
public interface IUserService {
public User selectUserById(Long custId);
public List<User> selectUsersByName(User user);
public void saveUser(User user);
public void updateUser(User user);
public void deleteUser(Long custId);
}
package com.zxh.customer.mapper.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zxh.customer.mapper.entity.User;
import com.zxh.customer.mapper.model.UserMapper;
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {
@Resource
private UserMapper userMapper;
public User selectUserById(Long custId) {
return this.userMapper.selectUserById(custId);
}
public List<User> selectUsersByName(User user) {
return this.userMapper.selectUsersByName(user);
}
public void saveUser(User user) {
this.userMapper.saveUser(user);
}
public void updateUser(User user) {
this.updateUser(user);
}
public void deleteUser(Long custId) {
this.deleteUser(custId);
}
}
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.zxh.customer.mapper" />
<context:property-placeholder
location="classpath:sysconfig/jdbc_config.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="jtorder" />
<property name="password" value="jtorder" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations"
value="classpath*:com/zxh/customer/mapper/model/**/*.xml" />
<property name="typeAliasesPackage" value="com.zxh.customer.mapper.entity" />
<property name="configLocation" value="classpath:sysconfig/Configuration.xml" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- MapperScannerConfigurer来帮我们自动扫描和注册Mapper接口,无需像下面单独设置,防止接口类很多的情况! 多个值得情况使用逗号、分号、空格分隔 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zxh.customer.mapper.model" />
</bean>
<!-- 数据映射器接口 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.zxh.customer.spring.dao.UserMapper"
/> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> -->
</beans>
<?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>
<!-- 设置mybatis3 运行时的行为方式 -->
<settings>
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间 -->
<setting name="defaultStatementTimeout" value="60000" />
<!-- 启用或禁用 缓存 -->
<setting name="cacheEnabled" value="false" />
<!-- 启用或禁用延迟加载。当禁用时, 所有关联对象都会即时加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 等等 -->
</settings>
</configuration>
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@172.16.1.241\:1521\:jtcrm
package springtest;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxh.customer.mapper.entity.User;
import com.zxh.customer.mapper.service.IUserService;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"sysconfig/applicationContext.xml");
IUserService userService = (IUserService) context
.getBean("userService");
// User user = userService.selectUserById(604763L);
// System.out.println(user.getCustName());
// Map map = new HashMap();
// map.put("custName", "测试测试");
// List<Map> list = userService.queryUsers(map);
// System.out.println(list.size());
User user = new User();
user.setCustName("封建礼教");
userService.saveUser(user);
// Map map = new HashMap();
// map.put("custId", "612035");
// try {
// userService.deleteUser(map);
// } catch (Exception e) {
// e.printStackTrace();
// }
System.out.println("成功");
}
}