1. 程式人生 > 資訊 >《艾爾登法環》遭大量劇透,黑客挖到數千份語音、對話檔案

《艾爾登法環》遭大量劇透,黑客挖到數千份語音、對話檔案


1.宣告式事務:AOP(不影響原有程式碼)


UserMapper介面:

package com.kakafa.mapper;
import com.kakafa.pojo.User;
import java.util.List;

public interface UserMapper {

        public List<User> selectUser();

        //新增一個使用者
        public int addUser(User user);

        //通過id刪除一個使用者
        public int deleteUser(int id);

}

UserMapper.xml:

<?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.kakafa.mapper.UserMapper">

    <select id="selectUser" resultType="user">
        select * from mybatistest.user;
    </select>

    <insert id="addUser" parameterType="user" >
        insert into mybatistest.user(id,name,pwd)
        values(#{id},#{name},#{pwd});
    </insert>

    <delete id="deleteUser" parameterType="int">
        delete from mybatistest.user where id=#{id};
    </delete>

</mapper>


UserMapperImpl:

package com.kakafa.mapper;
import com.kakafa.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class UserMapperImpl implements UserMapper{

    //我們所有操作都使用sqlSession來執行,現在都使用sqlSessionTemplate,因為習慣用sqlSession,所以這裡命名為sqlSession
    private SqlSessionTemplate sqlSession;

    //需要有set方法才能夠在spring的bean註冊中注入sqlSession的值-通過set方法注入:<property name="sqlSession" ref="sqlSession"/>
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        return users;
    }

    @Override
    public int addUser(User user) {
        return sqlSession.getMapper(UserMapper.class).addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return sqlSession.getMapper(UserMapper.class).deleteUser(id);
    }
}

spring-dao.xml:注意標頭檔案裡要加入aop和tx的東西

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatistest?useSSL=false&amp;useUnicode=true&amp;CharacterEncoding=UTF8&amp;serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--繫結MyBatis配置檔案-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/kakafa/mapper/UserMapper.xml"/>
    </bean>


    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="userMapperImpl" class="com.kakafa.mapper.UserMapperImpl">
        <property name="sqlSession" ref="sqlSession"/>
    </bean>

    <!--配置宣告式事務-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource"/>
    </bean>

    <!--結合AOP實現事務的織入-->
    <!--配置事務通知:-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--給哪些方法配置事務,以及配置事務的傳播特性propagation,預設是REQUIRED-->
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <!--*表示所有的方法-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事務的切入-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.kakafa.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>


</beans>


2.程式設計式事務:需要在程式碼中進行事務的管理