mybatis整合方式一:使用set方法注入sqlSession
1.导入jar包
- junit
- mybatis
- 数据库连接包
- spring-jdbc包
- spring-webmvc包
- aspectjweaver包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
2.编写配置文件
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--有了这个就不用写类的bean而是直接在类上标注@compent用@value为set方法赋值-->
<context:component-scan base-package="cn.xiong.pojo"></context:component-scan>
<aop:aspectj-autoproxy/>
<!-- DataSource:用spring代替mybatis配置-->
<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/mytest"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="configLocation" value="mybatis-config.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
mybatis-config.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">
<!-- mybatis的主配置文件 -->
<configuration>
<!-- 配置环境 -->
<typeAliases>
<package name="cn.xiong"></package>
</typeAliases>
<environments default="mysql">
<!-- 配置mysql的环境-->
<environment id="mysql">
<!-- 配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池) -->
<dataSource type="POOLED">
<!-- 配置连接数据库的4个基本信息 -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每个dao独立的配置文件 -->
<mappers>
<package name="cn.xiong.dao"></package>
</mappers>
</configuration>
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--有了这个就不用写类的bean而是直接在类上标注@compent用@value为set方法赋值-->
<import resource="spring.dao.xml"></import>
<bean id="UserDao" class="cn.xiong.dao.UserDao">
<property name="sqlSession" ref="sqlSession"></property>
</bean>
</beans>
3.实体类与doa
实体类
package cn.xiong.pojo;
/**
* @Author:优小熊
* @Description:Fightforyou
*/
public class User {
private int id;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private String sex;
private String address;
}
dao
public interface IUserDao {
public List<User> findUser();
}
---------------------------------
public class UserDao implements IUserDao {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<User> findUser() {
IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
return iUserDao.findUser();
}
}
dao.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="cn.xiong.dao.IUserDao">
<select id="findUser" resultType="User">
select * from user
</select>
</mapper>
4.测试
public class test01 {
private ApplicationContext context;
@Before//用于在测试方法执行之前执行
public void init() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void findUserTest() {
IUserDao iUserDao = context.getBean("UserDao", IUserDao.class);
iUserDao.findUser();
List<User> users = iUserDao.findUser();
for (User user : users) {
System.out.println(user);
}
}
}
mybatis整合二:继承SqlSessionDaoSupport
1.导入jar包
- junit
- mybatis
- 数据库连接包
- spring-jdbc包
- spring-webmvc包
- aspectjweaver包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
2.编写配置文件
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--有了这个就不用写类的bean而是直接在类上标注@compent用@value为set方法赋值-->
<context:component-scan base-package="cn.xiong.pojo"></context:component-scan>
<aop:aspectj-autoproxy/>
<!-- DataSource:用spring代替mybatis配置-->
<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/mytest"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="mybatis-config.xml"></property>
<property name="mapperLocations">
<list>
<value>classpath*:cn/xiong/dao/**/*.xml</value>
</list>
</property>
</bean>
</beans>
mybatis-config.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">
<!-- mybatis的主配置文件 -->
<configuration>
<!-- 配置环境 -->
<typeAliases>
<package name="cn.xiong"></package>
</typeAliases>
</configuration>
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--有了这个就不用写类的bean而是直接在类上标注@compent用@value为set方法赋值-->
<import resource="spring.dao.xml"></import>
<bean id="UserDao2" class="cn.xiong.dao.UserDao2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
3.实体类与doa
实体类
package cn.xiong.pojo;
/**
* @Author:优小熊
* @Description:Fightforyou
*/
public class User {
private int id;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
private String sex;
private String address;
}
dao
public interface IUserDao2 {
int insertUser(User user);
}
---------------------------------
public class UserDao2 extends SqlSessionDaoSupport implements IUserDao {
public int insertUser(User user) {
int i = getSqlSession().getMapper(IUserDao.class).insertUser(user);
return i;
}
}
dao.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="cn.xiong.dao.IUserDao">
<insert id="insertUser" parameterType="User">
insert into user (id,username,sex,address)values (#{id},#{username},#{sex},#{address})
</insert>
</mapper>
4.测试
public class test01 {
private ApplicationContext context;
@Before//用于在测试方法执行之前执行
public void init() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void testC(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao2 userDao = context.getBean("UserDao2", UserDao2.class);
User user = new User();
user.setId(49);
user.setUsername("dh");
user.setSex("女");
user.setAddress("hancheng");
int i = userDao.insertUser(user);
System.out.println(i);
}
}
优秀博客链接:https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247484144&idx=1&sn=768f97da78a9ceae8321d101da3c480e&scene=19#wechat_redirect