<version>5.3.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
[领取资料]( ) ### []( )编写实体类(需要导入lombok依赖)
package com.study.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;//加群1025684353一起吹水聊天 private String name; private String pwd;
}
### []( )编写实现接口
package com.study.mapper;
import com.study.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser(); //添加一个用户 int addUser(User user);//加群1025684353一起吹水聊天 //根据id删除用户 int deleteUser(int id);
}
[领取资料]( ) ### []( )编写配置文件 **UserMapper.xml** **注意:delete故意写错**<?xml version="1.0" encoding="UTF-8" ?>
<select id="selectUser" resultType="user"> select * from mybatis.user </select> <insert id="addUser" parameterType="user"> insert into mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd}) </insert> <delete id="deleteUser" parameterType="int"> deletes from mybatis.user where id=#{id} </delete>
**spring-dao.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: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.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/study/mapper/*.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean>
**mybatis-config.xml**<?xml version="1.0" encoding="UTF-8" ?>
<typeAliases> <package name="com.study.pojo"/> </typeAliases>
**applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userMapper" class="com.study.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
[领取资料]( ) ### []( )编写接口实现类
package com.study.mapper;
import com.study.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;//加群1025684353一起吹水聊天
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
@Override public List<User> selectUser() { User user =new User(30,"bbb","123456"); UserMapper mapper = getSqlSession().getMapper(UserMapper.class); mapper.addUser(user); mapper.deleteUser(30); return mapper.selectUser(); } @Override public int addUser(User user) { return getSqlSession().getMapper(UserMapper.class).addUser(user); } @Override public int deleteUser(int id) {//加群1025684353一起吹水聊天 return getSqlSession().getMapper(UserMapper.class).deleteUser(id); }
}
### []( )编写测试类 [领取资料]( )
import com.study.mapper.UserMapper;
import com.study.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyText {//加群1025684353一起吹水聊天
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = context.getBean("userMapper", UserMapper.class); List<User> userList = userMapper.selectUser(); for (User user : userList) { System.out.println(user); } }
}
### []( )测试 [领取资料]( ) **运行测试前数据库** [![image](https://www.www.zyiz.net/i/ll/?i=img_convert/965289111447421a7ebc560745f3d169.png)]( ) **运行测试出错** [![image](https://www.www.zyiz.net/i/ll/?i=img_convert/348c67d4aa1ed90e5609532dffbd91f9.png)]( ) **刷新数据库数据添加成功** [![image](https://www.www.zyiz.net/i/ll/?i=img_convert/e50b39e9fa6d05b4a3c9c90beef85ee3.png)]( ) **在程序开发中,这样的BUG重的,所以我们需要在spring中开启事务防止这样的漏洞发生** []( )在spring中使用事务 =========================================================================== ### [](https://gitee.com/vip204888/java-p7)一、在spring配置文件开启JDBC事务
<property name="dataSource" ref="dataSource" />
### [](https://gitee.com/vip204888/java-p7)二、配置好事务管理器后我们需要去配置事务的通知 [领取资料](https://gitee.com/vip204888/java-p7)
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
<tx:attributes> <!--给哪些方法配置事务--> <!--配置事务的传播特性 propagation:默认REQUIRED--> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <!--read-only:只读--> <tx:method name="select" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/>
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等…已整理上传,感兴趣的朋友可以看看支持一波!
<tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <!--read-only:只读--> <tx:method name="select" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/>
[外链图片转存中…(img-0fjAlKZf-1630680578445)]
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等…已整理上传,感兴趣的朋友可以看看支持一波!
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】