导入相关依赖
<?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">
<parent>
<artifactId>Spring_learn</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring_06_mybatis</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--数据库jar-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.16</version>
</dependency>
<!--spring连接数据库-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.16</version>
</dependency>
<!--aop织入-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.RC3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</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>
</dependencies>
</project>
编写配置文件
db.properties
注意:这里的url不能这样写否则出现报错:Malformed database URL, failed to parse the connection string near ‘;serverTimezone=UTC&useUnicode=true&characterEncoding=utf8’.**
url=jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
username=root
password=123456
mybatisConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--这里连接数据库在spring中写成DataSource-->
<!--<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!–数据库配置–>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.liu.mapper"/>
</mappers>-->
</configuration>
springMapper.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注册对象-->
<bean id="student" class="com.liu.pojo.Student"/>
<bean id="studentImpl" class="com.liu.mapper.StudentImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
springConfig.xml:配置数据源及注册sqlSession
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.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="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="db.properties"/>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!--注册SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
<property name="mapperLocations" value="classpath:com/liu/mapper/*.xml"/>
<!-- <property name="configurationProperties" value="db.properties"/>-->
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--配置事务-->
<!--启动事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织如-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务导入-->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.liu.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!--导入注册对象配置文件-->
<import resource="springMapper.xml"/>
</beans>
测试
StudentMapper.xml:mybatis编写sql语句页面
<?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.liu.mapper.StudentMapper">
<select id="selectStudent" resultMap="sMap">
select *
from student
</select>
<resultMap id="sMap" type="com.liu.pojo.Student">
<result property="sNO" column="studentNo"/>
<result property="sName" column="studentName"/>
<result property="sAge" column="studentAge"/>
</resultMap>
</mapper>
StudentMapper.java
package com.liu.mapper;
import com.liu.pojo.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentMapper {
//查
List<Student> selectStudent();
//增
int addStudent(Student stu);
//删
int deleteStudent(@Param("studentNo") String id);
//改
int updateStudent(@Param("studentNo") String id,@Param("studentName") String name);
}
StudentImpl.java:实现mybatis调用sqlSession来获取SQL执行后结果的操作
package com.liu.mapper;
import com.liu.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class StudentImpl implements StudentMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<Student> selectStudent() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.selectStudent();
}
@Override
public int addStudent(Student stu) {
return 0;
}
@Override
public int deleteStudent(String id) {
return 0;
}
@Override
public int updateStudent(String id, String name) {
return 0;
}
}
Mybatis.java
import com.liu.mapper.StudentMapper;
import com.liu.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Mybatis {
@Test
public void MyTest() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml");
StudentMapper studentImpl = context.getBean("studentImpl", StudentMapper.class);
List<Student> students = studentImpl.selectStudent();
System.out.println(students);
}
}