方式一
Object类
get和set,无参构造+有参构造
Pom.xml引入依赖
<!-- 打包方式jar-->
<packaging>jar</packaging>
<!--配置多个仓库 -->
<repositories>
<!-- Spring6 -->
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<!-- 依赖-->
<dependencies>
<!-- 引入Spring context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--引入mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!--引入mybatis-spring依赖-->
<!-- -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 引入德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<!-- Spring框架对Junit支持的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.0-M2</version>
<!--6.0.0-M2既支持junit4又支持junit5-->
</dependency>
<!--junit依赖-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>review-annotation</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- @Resource注解的依赖-->
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
Dao层
public interface TTTDao {
//查询所有
List<TTT11> selectAll();
//插入
int insert(TTT11 ttt11);
//通过id删除
int deleteById(int id);
//更新
int update(TTT11 ttt11);
}
myabtis-Mapper配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bank.dao.TTTDao">
<select id="selectAll" resultType="com.bank.object.TTT11">
select id,name,address from TTT11
</select>
<insert id="insert">
insert into TTT11 values(#{id},#{name},#{address})
</insert>
<delete id="deleteById" >
delete from TTT11 where id = #{id}
</delete>
<update id="update">
update TTT11 set address =#{address} where id=#{id}
</update>
</mapper>
编写jdbc.properties文件和Mybatis.xml核心配置文件
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://IP:3306/mysql?
jdbc.user= root
jdbc.password= 111111
Mybatis.xml主配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--settings控制mybatis全局行为-->
<settings>
<!-- logImpl设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
Service接口和serviceimp
接口
public interface TTTService {
int save(TTT11 ttt11);
int delecyById(int id);
List<TTT11> selectAll();
int update(TTT11 ttt11);
}
实现类
@Transactional
@Service("tTTService")
public class TTTServiceimpl implements TTTService {
@Resource
private TTTDao tttDao;
@Override
public int save(TTT11 ttt11) {
return tttDao.insert(ttt11);
}
@Override
public int delecyById(int id) {
return tttDao.deleteById(id);
}
@Override
public List<TTT11> selectAll() {
return tttDao.selectAll();
}
@Override
public int update(TTT11 ttt11) {
return tttDao.update(ttt11);
}
}
Spring.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:context="http://www.springframework.org/schema/context" 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/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">
<context:component-scan base-package="com.bank"></context:component-scan>
<!--引入外部文件-->
<context:property-placeholder location="jdbc.properties"/>
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dateSource"/>
<!--指定Mybatis-config.xml文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<!--注入别名-->
<property name="typeAliasesPackage" value="com.bank.object"/>
</bean>
<!--扫描mapper配置器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bank.dao"/>
</bean>
<!--事务管理器-->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"/>
</bean>
<!-- 启动事务注解-->
<tx:annotation-driven transaction-manager="TransactionManager"/>
</beans>
测试类
public class springMybatisTest {
@Test
public void sMybatisTest(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
TTTService tTTService = applicationContext.getBean("tTTService", TTTService.class);
tTTService.selectAll();
}
}
方式二
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring6mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 打包方式jar-->
<packaging>jar</packaging>
<!--配置多个仓库 -->
<repositories>
<!-- Spring6 -->
<repository>
<id>repository.spring.milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<!-- 依赖-->
<dependencies>
<!-- 引入Spring context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入Spring-jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.0-M2</version>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!--引入mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.11</version>
</dependency>
<!--引入mybatis-spring依赖-->
<!-- -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 引入德鲁伊连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
<!-- Spring框架对Junit支持的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.0-M2</version>
<!--6.0.0-M2既支持junit4又支持junit5-->
</dependency>
<!--junit依赖-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!--所在的目录-->
<directory>src/main/java</directory>
<includes>
<!--包括目录下的.properties .xml文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
实体类
get、set、toString、无参有参构造方法。
接口
public interface TTTDao {
//查询所有
List<TTT11> selectAll();
}
mapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.TTTDao">
<select id="selectAll" resultType="com.object.TTT11">
select id,name,address from TTT11
</select>
<insert id="insert">
insert into TTT11 values(#{id},#{name},#{address})
</insert>
<delete id="deleteById" >
delete from TTT11 where id = #{id}
</delete>
<update id="update">
update TTT11 set address =#{address} where id=#{id}
</update>
</mapper>
Service接口和实现类
public interface TTTService {
List<TTT11> selectAll();
}
public class TTTServiceimpl implements TTTService {
private TTTDao tttDao;
public TTTServiceimpl(TTTDao tttDao) {
this.tttDao = tttDao;
}
@Override
public List<TTT11> selectAll() {
return tttDao.selectAll();
}
}
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://IP:3306/mysql
jdbc.user= root
jdbc.password= 111111
mybatis-config.xml主配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- logImpl设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<mappers>
<package name="com.dao"/>
</mappers>
</configuration>
SpringConfig.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:context="http://www.springframework.org/schema/context" 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/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">
<!-- 组件扫描-->
<context:component-scan base-package="com"></context:component-scan>
<!--引入外部文件-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 声明数据来源-->
<bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactoryBean-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dateSource"/>
<!--指定Mybatis-config.xml主配置文件-->
<property name="configLocation" value="mybatis-config.xml"/>
<!--注入别名-->
<property name="typeAliasesPackage" value="com.object"/>
</bean>
<!--扫描mapper配置器 SqlSession.Mapper()-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="com.dao"/>
</bean>
<bean id="tttServiceimpl" class="com.service.TTTServiceimpl">
<!--构造注入-->
<constructor-arg index="0" ref="TTTDao"></constructor-arg>
</bean>
<!--事务管理器-->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"/>
</bean>
<!-- 启动事务注解-->
<tx:annotation-driven transaction-manager="TransactionManager"/>
</beans>
测试类
public class smtest {
@Test
public void test(){
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("SpringConfig.xml");
TTTService tttServiceimpl = applicationContext.getBean("tttServiceimpl", TTTService.class);
tttServiceimpl.selectAll();
}
}
标签:www,http,spring,springframework,Mybatis3.5,手牵手,mybatis,org,Spring6 From: https://blog.51cto.com/u_15081423/6106452