1.简介
在应用程序注册中,如果把用户分为管理员和普通用户。管理员注册的时候(加入能够自己注册),那么需要把人和角色一起注册,单单只是人或者角色注册都是失败的。本博客通过一个例子解决这个问题。
2.Aop编程环境搭建
这是笔者用到的jar包,另外笔者用的是jdk1.8,下面是笔者的项目目录
废话少说,上代码
3.编写Java bean
Blogger 类·
package com.haichuang.beans;
import java.io.Serializable;
public class Blogger implements Serializable {
public Blogger() {
super();
// TODO Auto-generated constructor stub
}
public Blogger(String name, String password, String nickName) {
super();
this.name = name;
this.password = password;
this.nickName = nickName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "Blogger [id=" + id + ", name=" + name + ", password=" + password + ", nickName=" + nickName + "]";
}
private Integer id;
private String name;
private String password;
private String nickName;
}
package com.haichuang.beans;
import java.io.Serializable;
public class UserType implements Serializable {
public UserType() {
super();
// TODO Auto-generated constructor stub
}
public UserType(Integer isAdmin) {
super();
this.isAdmin = isAdmin;
}
public Integer getBloggerId() {
return bloggerId;
}
public void setBloggerId(Integer bloggerId) {
this.bloggerId = bloggerId;
}
public Integer isAdmin() {
return isAdmin;
}
public void setAdmin(Integer isAdmin) {
this.isAdmin = isAdmin;
}
@Override
public String toString() {
return "UserType [bloggerId=" + bloggerId + ", isAdmin=" + isAdmin + "]";
}
private Integer bloggerId;
private Integer isAdmin;
}
4.打开Navicat建立表
blogger表
usertype表
5.service层
创建接口
package com.haichuang.services;
import java.sql.SQLException;
import com.haichuang.beans.Blogger;
import com.haichuang.beans.UserType;
import com.haichuang.exceptions.UserException;
public interface IUserService {
public void addUser(Blogger blogger,UserType userTpe) throws SQLException, UserException;
}
实现类
package com.haichuang.services;
import java.sql.SQLException;
import com.haichuang.beans.Blogger;
import com.haichuang.beans.UserType;
import com.haichuang.daos.IBloggerDao;
import com.haichuang.daos.IUserTypeDao;
import com.haichuang.exceptions.UserException;
public class UserServiceImpl implements IUserService {
@Override
public void addUser(Blogger blogger, UserType userType) throws SQLException, UserException {
// TODO Auto-generated method stub
if(blogger.getName()==null||blogger.getPassword()==null||blogger.getNickName()==null)
throw new UserException("未注册用户");
int id=bdao.insertBlogger(blogger);
userType.setBloggerId(id);
if(userType.isAdmin()!=null&&userType.isAdmin().intValue()!=0&&
userType.isAdmin().intValue()!=1) throw new UserException("未注册用户角色");
utdao.insertUserType(userType);
}
public void setBdao(IBloggerDao bdao) {
this.bdao = bdao;
}
public void setUtdao(IUserTypeDao utdao) {
this.utdao = utdao;
}
private IBloggerDao bdao;
private IUserTypeDao utdao;
}
6.dao层
IBloggerDao接口
package com.haichuang.daos;
import java.sql.SQLException;
import com.haichuang.beans.Blogger;
public interface IBloggerDao {
int insertBlogger(Blogger blogger) throws SQLException;
}
其实现类
package com.haichuang.daos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import com.haichuang.beans.Blogger;
public class BloggerDaoImpl extends JdbcDaoSupport implements IBloggerDao{
@Override
public int insertBlogger(Blogger blogger) throws SQLException
{
KeyHolder keyHolder = new GeneratedKeyHolder();
this.getJdbcTemplate().update(
new PreparedStatementCreator(){
public java.sql.PreparedStatement createPreparedStatement(Connection conn) throws SQLException{
String sql="insert into blogger(name,password,nickName) values(?,?,?)";
java.sql.PreparedStatement ps = conn.prepareStatement(sql);
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1,blogger.getName());
ps.setString(2,blogger.getPassword());
ps.setString(3,blogger.getNickName());
return ps;
}
},
keyHolder);
int id=keyHolder.getKey().intValue();
blogger.setId(id);
return id;
}
}
IUserTypeDao 接口
package com.haichuang.daos;
import com.haichuang.beans.UserType;
public interface IUserTypeDao {
void insertUserType(UserType userType);
}
其实现类
package com.haichuang.daos;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.haichuang.beans.UserType;
public class UserTypeDaoImpl extends JdbcDaoSupport implements IUserTypeDao {
@Override
public void insertUserType(UserType userType) {
String sql="insert into usertype(bloggerId,isAdmin) values(?,?)";
this.getJdbcTemplate().update(sql,userType.getBloggerId(),userType.isAdmin());
}
}
7.这里我们需要使用自定义异常
package com.haichuang.exceptions;
public class UserException extends Exception {
public UserException() {
super();
// TODO Auto-generated constructor stub
}
public UserException(String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}
8.xml文件配置
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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.xsd">
<!-- 注册数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 注册dao -->
<bean id="bloggerDaoImpl" class="com.haichuang.daos.BloggerDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册dao -->
<bean id="userTypeDaoImpl" class="com.haichuang.daos.UserTypeDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册service -->
<bean id="userServiceImpl" class="com.haichuang.services.UserServiceImpl">
<property name="bdao" ref="bloggerDaoImpl"/>
<property name="utdao" ref="userTypeDaoImpl"/>
</bean>
下面是AspectJ的配置,对UserException异常进行回滚
<!-- ========aop======== -->
<!-- 注册事务管理器 -->
<bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- aop注册事务通知 -->
<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
<tx:attributes>
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED"/>
<tx:method name="add*" isolation="READ_COMMITTED" propagation="REQUIRED" rollback-for="UserException"/>
</tx:attributes>
</tx:advice>
<!-- aop配置 -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* *..services.*.*(..))" id="myPointcut"/>
<aop:pointcut expression="execution(* *..daos.*.*(..))" id="myPointcut2"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut2"/>
</aop:config>
</beans>
9.测试类
package com.haichuang.tests;
import java.sql.SQLException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.haichuang.beans.Blogger;
import com.haichuang.beans.UserType;
import com.haichuang.exceptions.UserException;
import com.haichuang.services.IUserService;
import com.haichuang.services.UserServiceImpl;
public class Demo {
@Before
public void bofore()
{
String resource="applicationContext.xml";
ac=new ClassPathXmlApplicationContext(resource);
}
@Test
public void test() throws SQLException, UserException
{
IUserService service=(IUserService) ac.getBean("userServiceImpl");
service.addUser(new Blogger("liu","123456","123"), new UserType(2));
}
private ApplicationContext ac;
}