实体类:Emp.java
01 |
package com.lixing.scm.entity;
|
02 |
03 |
public class Emp {
|
04 |
private String id;
|
05 |
private String name;
|
06 |
private String sex;
|
07 |
private int age;
|
08 |
private String phone;
|
09 |
public String getId() {
|
10 |
return id;
|
11 |
}
|
12 |
public void setId(String id) {
|
13 |
this.id = id;
|
14 |
}
|
15 |
public String getName() {
|
16 |
return name;
|
17 |
}
|
18 |
public void setName(String name) {
|
19 |
this.name = name;
|
20 |
}
|
21 |
public String getSex() {
|
22 |
return sex;
|
23 |
}
|
24 |
public void setSex(String sex) {
|
25 |
this.sex = sex;
|
26 |
}
|
27 |
public int getAge() {
|
28 |
return age;
|
29 |
}
|
30 |
public void setAge(int age) {
|
31 |
this.age = age;
|
32 |
}
|
33 |
public String getPhone() {
|
34 |
return phone;
|
35 |
}
|
36 |
public void setPhone(String phone) {
|
37 |
this.phone = phone;
|
38 |
}
|
39 | } |
定义实体内操作接口:EmpMapper.java
01 |
package com.lixing.scm.test.mapper;
|
02 |
03 |
import java.util.List;
|
04 |
import java.util.Map;
|
05 |
06 |
import com.lixing.scm.entity.Emp;
|
07 |
08 |
public interface EmpMapper {
|
09 |
void insertEmp(Emp emp);
|
10 |
List<Emp> getAllEmp();
|
11 |
Emp getById(String id);
|
12 |
void deleteEmp(String id);
|
13 |
void updateEmp(Map<String,Object> map);
|
14 | } |
[代码] 定义实体类操作接口的映射文件:EmpMapper.xml
01 |
<?xml version="1.0" encoding="UTF-8" ?>
|
02 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
03 |
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
04 |
<mapper namespace="com.lixing.scm.test.mapper.EmpMapper">
|
05 |
<parameterMap type="com.lixing.scm.entity.Emp" id="parameterMapEmp">
|
06 |
<parameter property="id"/>
|
07 |
08 |
<parameter property="name"/>
|
09 |
<parameter property="sex"/>
|
10 |
<parameter property="age"/>
|
11 |
<parameter property="phone"/>
|
12 |
</parameterMap>
|
13 |
|
14 |
<resultMap type="com.lixing.scm.entity.Emp" id="resultMapEmp">
|
15 |
16 |
<result property="id" column="id"/>
|
17 |
<result property="name" column="name"/>
|
18 |
<result property="sex" column="sex"/>
|
19 |
<result property="age" column="age"/>
|
20 |
<result property="phone" column="phone"/>
|
21 |
</resultMap>
|
22 |
23 |
|
24 |
<insert id="insertEmp" parameterMap="parameterMapEmp">
|
25 |
INSERT INTO emp(id,name,sex,age,phone)
|
26 |
VALUES(?,?,?,?,?)
|
27 |
</insert>
|
28 |
<select id="getAllEmp" resultMap="resultMapEmp">
|
29 |
SELECT * FROM emp
|
30 |
</select>
|
31 |
<select id="getById" parameterType="String" resultMap="resultMapEmp">
|
32 |
33 |
SELECT * FROM emp
|
34 |
WHERE id=#{value}
|
35 |
</select>
|
36 |
<delete id="deleteEmp" parameterType="String">
|
37 |
DELETE FROM emp
|
38 |
WHERE id=#{value}
|
39 |
</delete>
|
40 |
<update id="updateEmp" parameterType="java.util.Map">
|
41 |
UPDATE emp
|
42 |
SET name=#{name},sex=#{sex},age=#{age},phone=#{phone}
|
43 |
WHERE id=#{id}
|
44 |
</update>
|
45 |
46 |
</mapper>
|
Spring3.0.6定义:applicationContext.xml
01 |
<?xml version="1.0" encoding="UTF-8"?>
|
02 |
<beans xmlns="http://www.springframework.org/schema/beans"
|
03 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
04 |
xmlns:context="http://www.springframework.org/schema/context"
|
05 |
xmlns:aop="http://www.springframework.org/schema/aop"
|
06 |
xmlns:tx="http://www.springframework.org/schema/tx"
|
07 |
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
08 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
09 |
http://www.springframework.org/schema/context
|
10 |
http://www.springframework.org/schema/context/spring-context-3.0.xsd
|
11 |
http://www.springframework.org/schema/tx
|
12 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
|
13 |
http://www.springframework.org/schema/aop
|
14 |
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
|
15 |
16 |
<!-- -->
|
17 |
18 |
<context:annotation-config />
|
19 |
<context:component-scan base-package="com.lixing.scm.test.*" />
|
20 |
21 |
22 |
<!-- jdbc.propertis Directory -->
|
23 |
<bean
|
24 |
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
25 |
<property name="locations" value="classpath:jdbc.properties" />
|
26 |
27 |
</bean>
|
28 |
29 |
<bean id="MyDataSource" destroy-method="close"
|
30 |
class="org.apache.commons.dbcp.BasicDataSource">
|
31 |
<property name="driverClassName" value="${jdbc.driverClassName}" />
|
32 |
<property name="url" value="${jdbc.url}" />
|
33 |
<property name="username" value="${jdbc.username}" />
|
34 |
<property name="password" value="${jdbc.password}" />
|
35 |
36 |
</bean>
|
37 |
38 |
<!-- SqlSessionFactory -->
|
39 |
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
40 |
<property name="dataSource" ref="MyDataSource" />
|
41 |
</bean>
|
42 |
<!-- ScanMapperFiles -->
|
43 |
44 |
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
45 |
<property name="basePackage" value="com.lixing.scm.test.mapper" />
|
46 |
</bean>
|
47 |
48 |
49 |
50 |
|
51 |
|
52 |
<property name="dataSource" ref="MyDataSource"></property>
|
53 |
54 |
</bean>
|
55 |
|
56 |
<tx:advice id="userTxAdvice" transaction-manager="transactionManager">
|
57 |
<tx:attributes>
|
58 |
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
|
59 |
|
60 |
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
|
61 |
rollback-for="java.lang.RuntimeException" />
|
62 |
<tx:method name="update*" propagation="REQUIRED" read-only="false"
|
63 |
rollback-for="java.lang.Exception" />
|
64 |
65 |
|
66 |
<tx:method name="find*" propagation="SUPPORTS"/>
|
67 |
<tx:method name="get*" propagation="SUPPORTS"/>
|
68 |
<tx:method name="select*" propagation="SUPPORTS"/>
|
69 |
</tx:attributes>
|
70 |
</tx:advice>
|
71 |
|
72 |
<aop:config>
|
73 |
|
74 |
75 |
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
|
76 |
</aop:config>
|
77 |
|
78 |
|
79 |
<!-- 以下为自定义Bean-->
|
80 |
<bean id="empDao" class="com.lixing.scm.test.dao.impl.EmpDaoImpl"
|
81 |
autowire="byName" />
|
82 |
|
83 |
</beans>
|
DAO接口:EmpDAO.java
01 |
package com.lixing.scm.test.dao;
|
02 |
03 |
import java.util.List;
|
04 |
import java.util.Map;
|
05 |
06 |
import com.lixing.scm.entity.Emp;
|
07 |
08 |
public interface EmpDao {
|
09 |
void insertEmp(Emp emp);
|
10 |
List<Emp> getAllEmp();
|
11 |
Emp getById(String id);
|
12 |
void deleteEmp(String id);
|
13 |
void updateEmp(Map<String, Object> map);
|
14 | } |
DAO接口实现类:EmpDaoImpl.java
01 |
package com.lixing.scm.test.dao.impl;
|
02 |
03 |
import java.util.List;
|
04 |
import java.util.Map;
|
05 |
06 |
import com.lixing.scm.entity.Emp;
|
07 |
import com.lixing.scm.test.dao.EmpDao;
|
08 |
import com.lixing.scm.test.mapper.EmpMapper;
|
09 |
10 |
public class EmpDaoImpl implements EmpDao {
|
11 |
private EmpMapper empMapper; //在此处注入一个empMapper
|
12 |
|
13 |
@Override
|
14 |
public void insertEmp(Emp emp) {
|
15 |
this.empMapper.insertEmp(emp);
|
16 |
throw new RuntimeException("Error"); //测试抛出RuntimeException //异常查看数据库是否存在记录
|
17 |
}
|
18 |
19 |
@Override
|
20 |
public void deleteEmp(String id) {
|
21 |
this.empMapper.deleteEmp(id);
|
22 |
}
|
23 |
24 |
@Override
|
25 |
public List<Emp> getAllEmp() {
|
26 |
return this.empMapper.getAllEmp();
|
27 |
}
|
28 |
29 |
@Override
|
30 |
public Emp getById(String id) {
|
31 |
return this.empMapper.getById(id);
|
32 |
}
|
33 |
34 |
@Override
|
35 |
public void updateEmp(Map<String, Object> map) {
|
36 |
this.empMapper.updateEmp(map);
|
37 |
}
|
38 |
39 |
|
40 |
public EmpMapper getEmpMapper() {
|
41 |
return empMapper;
|
42 |
}
|
43 |
44 |
public void setEmpMapper(EmpMapper empMapper) {
|
45 |
this.empMapper = empMapper;
|
46 |
}
|
47 | } |
Service层接口:EmpService.java
1 |
package com.lixing.scm.test.service;
|
2 |
3 |
import com.lixing.scm.entity.Emp;
|
4 |
5 |
public interface EmpService {
|
6 |
void insertEmp(Emp emp);
|
7 | } |
Service层接口实现类:EmpServiceImpl.java
01 |
package com.lixing.scm.test.service.impl;
|
02 |
03 |
import com.lixing.scm.entity.Emp;
|
04 |
import com.lixing.scm.test.dao.EmpDao;
|
05 |
import com.lixing.scm.test.service.EmpService;
|
06 |
07 |
public class EmpServiceImpl implements EmpService {
|
08 |
private EmpDao empDao;
|
09 |
10 |
@Override
|
11 |
public void insertEmp(Emp emp) {
|
12 |
empDao.insertEmp(emp);
|
13 |
14 |
}
|
15 |
16 |
public EmpDao getEmpDao() {
|
17 |
return empDao;
|
18 |
}
|
19 |
20 |
public void setEmpDao(EmpDao empDao) {
|
21 |
this.empDao = empDao;
|
22 |
}
|
23 | } |
测试类:TestEmpService.java
01 |
import org.junit.Test;
|
02 |
import org.springframework.context.ApplicationContext;
|
03 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
04 |
05 |
import com.lixing.scm.entity.Emp;
|
06 |
import com.lixing.scm.test.service.EmpService;
|
07 |
08 |
09 |
public class TestEmpService {
|
10 |
@Test
|
11 |
public void testTrasaction(){
|
12 |
Emp emp=new Emp();
|
13 |
emp.setId("00000003");
|
14 |
emp.setName("某某某");
|
15 |
emp.setAge(50);
|
16 |
emp.setSex("男");
|
17 |
emp.setPhone("566666");
|
18 |
|
19 |
|
20 |
EmpService service=ctx.getBean(EmpService.class);
|
21 |
service.insertEmp(emp);
|
22 |
}
|
23 | } |
本文详细介绍了企业级Java应用中实体类Emp的设计及其对应的持久化操作接口EmpMapper的定义,包括数据库操作的映射文件EmpMapper.xml。同时展示了Spring3.0.6环境下配置文件applicationContext.xml如何整合MyBatis与数据库交互,实现了实体类的操作接口。通过自定义Bean实现了DAO层接口EmpDao的实现和Service层接口EmpService的调用,确保了业务逻辑的封装和事务管理。
1953

被折叠的 条评论
为什么被折叠?



