文章目录
servicedaoaspect横切(核心方法)DruidUtil测试
service
public interface UserService {
Integer
insertByTsbId(String tsbName
, String ssbName
) throws SQLException
;
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper
;
@Override
public Integer
insertByTsbId(String tsbName
, String ssbName
) throws SQLException
{
return userMapper
.insertByTsbId(tsbName
,ssbName
);
}
}
dao
public interface UserMapper {
Integer
insertByTsbId(String tsbName
, String ssbName
) throws SQLException
;
}
@Repository
public class UserMapperImpl implements UserMapper {
@Override
public Integer
insertByTsbId(String tsbName
, String ssbName
) throws SQLException
{
Connection conn
= null
;
PreparedStatement ps
= null
;
ResultSet rs
= null
;
Integer result
= null
;
try {
conn
= DruidUtil
.getConnection();
ps
= conn
.prepareStatement("INSERT INTO system_top_sort_book(tsb_name) VALUES (?)", Statement
.RETURN_GENERATED_KEYS
);
ps
.setObject(1, tsbName
);
ps
.executeUpdate();
rs
= ps
.getGeneratedKeys();
long tsbId
= 0;
if (rs
.next()) {
tsbId
= rs
.getLong(1);
}
ps
= conn
.prepareStatement("INSERT INTO system_second_sort_book(tsb_id,ssb_name) VALUES (?,?)");
ps
.setObject(1, tsbId
);
ps
.setObject(2, ssbName
);
result
= ps
.executeUpdate();
}finally {
DruidUtil
.close(null
, ps
, rs
);
}
return result
;
}
}
aspect横切(核心方法)
@Component
@Aspect
public class RoutineAspect {
@Pointcut("execution(* com.javasm.service.impl.UserServiceImpl.insertByTsbId(..))")
public void servicePointCut() {
}
@Around("servicePointCut()")
public Object
routineManege(ProceedingJoinPoint jp
) {
Object result
= null
;
Connection conn
= null
;
try {
conn
= DruidUtil
.getConnection();
conn
.setAutoCommit(false);
System
.out
.println("执行了前置通知,设置conn自动提交为false");
result
= jp
.proceed();
conn
.commit();
System
.out
.println("执行了返回通知,设置conn提交");
} catch (Throwable throwable
) {
throwable
.printStackTrace();
try {
conn
.rollback();
System
.out
.println("执行了异常通知,设置conn回滚");
} catch (SQLException e
) {
e
.printStackTrace();
}
} finally {
DruidUtil
.close(conn
);
System
.out
.println("执行了最终通知,设置conn关闭");
}
return result
;
}
}
DruidUtil
public class DruidUtil {
private DruidUtil() {
}
private static DataSource dataSource
= null
;
private static final ThreadLocal
<Connection> CONNECTION_THREAD_LOCAL
= new ThreadLocal<>();
static {
try {
Properties p
= new Properties();
p
.load(DruidUtil
.class.getClassLoader().getResourceAsStream("druid.properties"));
DruidDataSourceFactory druidDataSourceFactory
= new DruidDataSourceFactory();
dataSource
= druidDataSourceFactory
.createDataSource(p
);
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static Connection
getConnection() throws SQLException
{
Connection connection
= CONNECTION_THREAD_LOCAL
.get();
try {
if (connection
== null
|| connection
.isClosed()) {
connection
= dataSource
.getConnection();
CONNECTION_THREAD_LOCAL
.set(connection
);
}
} catch (SQLException e
) {
e
.printStackTrace();
}
return connection
;
}
public static DataSource
getDataSource(){
return dataSource
;
}
public static void close(Connection connection
){
try {
if (connection
!= null
) {
connection
.close();
CONNECTION_THREAD_LOCAL
.remove();
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static void close(Connection connection
, Statement statement
, ResultSet resultSet
) {
try {
if (resultSet
!= null
) {
resultSet
.close();
}
} catch (Exception e
) {
e
.printStackTrace();
}
try {
if (statement
!= null
) {
statement
.close();
}
} catch (Exception e
) {
e
.printStackTrace();
}
try {
if (connection
!= null
) {
connection
.close();
CONNECTION_THREAD_LOCAL
.remove();
}
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
测试
public class RoutineTest {
public static void main(String
[] args
) {
ApplicationContext ac
= new ClassPathXmlApplicationContext("applicationContext.xml");
UserService bean
= ac
.getBean(UserService
.class);
try {
System
.out
.println(bean
.insertByTsbId("111", null
));
} catch (SQLException e
) {
e
.printStackTrace();
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-37680.html