mybatis spring 全註解配置
阿新 • • 發佈:2019-01-02
pom引入jar包
<!-- mybatis jar start --> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency>
spring配置
<!--匯入外部配置檔案 --> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/> <!-- 使用annotation 自動註冊bean, 並保證@Required、@Autowired的屬性被注入 --> <!-- spring註解掃描包 --> <context:component-scan base-package="com.services" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 資料來源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.mysql.driverClassName}" /> <property name="url" value="${jdbc.mysql.url}" /> <property name="username" value="${jdbc.mysql.username}" /> <property name="password" value="${jdbc.mysql.password}" /> <!-- 初始化連線大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連線池最大數量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連線池最大空閒 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連線池最小空閒 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連線最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> <!-- 禁止預設自動提交 --> <property name="defaultAutoCommit"> <value>false</value> </property> <!-- 開啟測試連線 --> <property name="testWhileIdle"> <value>true</value> </property> <property name="testOnBorrow"> <value>true</value> </property> <property name="validationQuery"> <value>select 1 from dual</value> </property> <property name="timeBetweenEvictionRunsMillis"> <value>300000</value> </property> <property name="numTestsPerEvictionRun"> <value>3</value> </property> <property name="minEvictableIdleTimeMillis"> <value>600000</value> </property> </bean> <!-- spring和MyBatis整合 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis-config.xml --> <property name="configLocation" value="classpath:spring/mybatis-config.xml"/> </bean> <!-- DAO介面所在包名,Spring會自動查詢其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.daos" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 註解事務掃描 --> <tx:annotation-driven transaction-manager="transactionManager"/>
jdbc.properties 資料庫配置檔案
###mysql表
jdbc.mysql.driverClassName=com.mysql.jdbc.Driver
#[ 測試用 ]
jdbc.mysql.url=jdbc:mysql://10.143.131.52:3306/test
jdbc.mysql.username=test
jdbc.mysql.password=test
#初始化連線大小
initialSize=3
#連線池最大數量
maxActive=50
#連線池最大空閒
maxIdle=10
#連線池最小空閒
minIdle=0
#獲取連線最大等待時間
maxWait=-1
使用方法
例:查詢
實體類
package com.dto;
public class TestDTO {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DAO層
package com.daos;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.dto.TestDTO;
@Mapper
public interface TestDAO {
@Select("<script> " +
"select " +
"t.ID as id " +
",t.NAME as name " +
",t.AGE as age " +
"from " +
"${tablename} t " +
"where " +
"t.age in " +
"<foreach item='item' index='index' collection='ages' open='(' separator=',' close=')'> " +
"#{item} " +
"</foreach> " +
"</script> "
)
List<TestDTO> list(@Param("tablename")String tablename, @Param("ages")List<int> ages);
}
service層介面
package com.services;
import java.util.List;
import com.dto.TestDTO;
public interface TestService {
/**
* 資訊
* @param
*/
public List<TestDTO> list(String tablename, List<int> ages);
}
service層實現
package com.services.impl;
import java.util.List;
import com.daos.TestDAO;
import com.dto.TestDTO;
import com.services.TestService;
@Service
public class TestService implements TestService{
@Autowired
private TestDAO testDAO;
/**
* 資訊
* @param
*/
public List<TestDTO> list(String tablename, List<int> ages){
try {
return testDAO.list(tablename, ages);
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<TestDTO>();
}
}
controller層
package com.web;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.daos.TestDAO;
import com.dto.TestDTO;
import com.services.TestService;
/**
* @功能 處理相關請求
* @author zjw
*
*/
@Controller
@RequestMapping("/test")
public class TestController extends BaseController {
private static final Logger log = LoggerFactory
.getLogger(TestController.class);
@Autowired
private TestService testServiceImpl;
/************************************* 任務管理 **********************************/
/**
* @functional 頁面跳轉
* @param model
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/test.do")
public String test(Model model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<int> ageList = new ArrayList<>();
mobileList.add(80);
List<TestDTO> tests = testServiceImpl.list("t_test", ageList);
System.out.println("tests:"+tests+":"+tests.size());
return "/view/test/listTest";
}
}