基于传统Dao方式实现spring+Mybatis的整合
创建并编写Account实体类
//省略import
public class Account {
private Integer a_id;
private String a_name;
//省略getter setter方法 构造器
}
创建并编写dao层AccountDao接口
//省略import
public interface AccountDao {
int insertAccount(Account account);
}
创建并编写dao层AccountDao接口实现类 便于传统dao方式使用
//省略import
public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao {
@Override
public int insertAccount(Account account) {
return this.getSqlSession().insert("edu.gdkm.dao.AccountDao.insertAccount",account);
}
}
创建并编写dao层AccountDao接口对象的映射文件
<?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="edu.gdkm.dao.AccountDao">
<select id="getAccountList" resultType="edu.gdkm.po.Account" >
select * from account
</select>
</mapper>
创建并编写服务层AccountService接口
//省略import
public interface AccountService {
int insertAccount(Account account) ;
}
编写服务层AccountService接口实现类
//省略import
@Service
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public int insertAccount(Account account) {
return accountDao.insertAccount(account);
}
}
编写mybatis配置文件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">
<configuration>
<mappers>
<mapper resource="edu/gdkm/dao/AccountDao.xml"/>
</mappers>
</configuration>
创建并编写spring配置文件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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="accountDao" class="edu.gdkm.dao.impl.AccountDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="accountService" class="edu.gdkm.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
</beans>
编写测试类Test
public class Test {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) act.getBean("accountService");
int row = accountService.insertAccount(new Account(8, "account8"));
System.out.println(row);
}
}
基于Mapper接口实现spring+Mybatis的整合
方式一:MapperFactoryBean方式
编写Account实体类
//省略import
public class Account {
private Integer a_id;
private String a_name;
//省略getter setter方法 构造器
}
编写dao接口
//省略import
public interface AccountDao {
int insertAccount(Account account);
}
编写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="edu.gdkm.dao.AccountDao">
<insert id="insertAccount" parameterType="edu.gdkm.pojo.Account">
insert into account (a_id,a_name) values (#{a_id},#{a_name});
</insert>
</mapper>
编写AccountService接口
//省略import
public interface AccountService {
int insertAccount(Account account) ;
}
修改实现类AccountServiceImpl
//省略import
@Service
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public int insertAccount(Account account) {
return accountDao.insertAccount(account);
}
}
创建并编写spring配置文件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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<bean id="accountDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="edu.gdkm.dao.AccountDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>-->
<bean id="accountService" class="edu.gdkm.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
</beans>
编写测试类Test
public class ServiceTest {
public static void main(String[] args) throws Exception {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) act.getBean("accountService");
int row = accountService.insertAccount(new Account(33, "33"));
System.out.println(row);
}
}
方式二:MapperScannerConfigurer方式
编写实体Account
public class Account {
private Integer a_id;
private String a_name;
}
编写AccountDao接口
public interface AccountDao {
int insertAccount(Account account);
}
编写AccountDao.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="edu.gdkm.dao.AccountDao">
<insert id="insertAccount" parameterType="edu.gdkm.pojo.Account">
insert into account (a_id,a_name) values (#{a_id},#{a_name});
</insert>
</mapper>
编写AccountService接口
public interface AccountService {
int insertAccount(Account account) ;
}
编写AccountService实现类AccountServiceImpl
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao; //注解注入
@Override
public int insertAccount(Account account) {
return accountDao.insertAccount(account);
}
}
编写测试类Test
public class Test {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) act.getBean("accountService");
int row = accountService.insertAccount(new Account(8, "account8"));
System.out.println(row);
}
}