mybatis與spring的整合(代碼實現)java
須要jar包:
spring
mybatis核心包;依賴包;log4j包;spring croe;beans;tx;aop;aspects;context;expression;jdbc; mybatis和spring得整合jar包;sql
環境配置:數據庫
1)mybatis.xml和mapper.xml配置在第一節有express
2)spring.xml配置在第二節有apache
當配置好全部得環境,引入jar包後,開始寫代碼。api
1.先配置mybatis.xml文件:mybatis
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.bean" />
</typeAliases>
</configuration>
2.如今用spring.xml配置,-->開啓自動掃描(註釋)-->properties文件得placeholder-->鏈接數據庫app
<context:component-scan base-package="com.service" /> <context:property-placeholder location="classpath:db.properties" /> <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${Adriver}"></property> <property name="JdbcUrl" value="${Aurl}"></property> <property name="user" value="${Ausername}"></property> <property name="password" value="${Apassword}"></property> <property name="maxPoolSize" value="10"></property> <property name="initialPoolSize" value="3"></property> </bean>
3.寫一個bean(UsersBean)ide
package com.bean; public class UsersBean { private int uid; private String uname; private String upass; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpass() { return upass; } public void setUpass(String upass) { this.upass = upass; } }
4.寫一個UsersDao,對數據庫數據進行操做
package com.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.bean.UsersBean; public interface IUsersDao { public List<UsersBean> queryAll(); }
5.寫一個mapper.xml映射文件(IUsersDao.xml)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" > <mapper namespace="com.dao.IUsersDao"> <select id="queryAll" resultType="usersbean"> select * from users </select> </mapper>
6.spring.xml配置,sqlSessionFactory()建立,mapper.xml文件得映射
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <property name="dataSource" ref="datasource"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.dao"></property> <property name="sqlSessionFactoryBeanName" value="factory"></property> </bean>
7.UsersServiceImpl類繼承IUsersService ,其中:(@Resource(name="IUsersDao"))name屬性是值service層和dao層得關聯
通常若是dao層mapper.xml文件名爲UserDao,則name="userDao",不寫就是默認爲userDao.但我這裏得name="IUserDao".
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.bean.UsersBean; import com.dao.IUsersDao; import com.service.IUsersService; @Service public class UsersServiceImpl implements IUsersService { @Resource private IUsersDao usersDao; @Override public List<UsersBean> findAll() { // TODO Auto-generated method stub return usersDao.queryAll(); } }
在這裏遇到了一個問題 ,在spring.xml中我寫的是批量配置使用了SqlSessionFactoryBean 本@(Resource name="userDao")第一個字母小寫,多是name="iUserDao"或者name="IuserDao",但都是錯誤得,由於我用的是以I開頭的接口名,而後(@Resource(name="IUsersDao")是這樣的。這是一個問題我不太懂,不懂它底層是怎樣實現,但願你們懂得能夠留言,咱們逐一討論。
8.測試;(junit)
package test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.UsersBean; import com.service.impl.UsersService; class Test { @org.junit.jupiter.api.Test void test() { // 實例化UsersService對象,調用顯示方法 ApplicationContext app = new ClassPathXmlApplicationContext("spring/spring.xml"); UsersService us = app.getBean("UsersService", UsersService.class); List<UsersBean> list = us.findAll(); for (UsersBean user : list) { if (list != null) { System.out.println("uid:" + user.getUid() + "\tuname:" + user.getUname() + "\tupwd:" + user.getUpwd()); } } System.out.println(app); } }
ok!!
多作筆記,加深記憶!!!
學無止境。