@[toc]java
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.sxt</groupId> <artifactId>mybatis-17-spring</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.21.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- mybatis和spring整合包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.3.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.0</version> </dependency> </dependencies> </project>
<?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> <settings> <!-- 開啓延遲加載 --> <setting name="lazyLoadingEnabled" value="true" /> <setting name="aggressiveLazyLoading" value="false" /> </settings> </configuration>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 關聯數據屬性文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 開啓掃描 --> <context:component-scan base-package="com.sxt.service.impl"/> <!-- 配置數據源 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource" > <property name="driverClass" value="${driver}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="user" value="${names}"></property> <property name="password" value="${password}"></property> </bean> <!-- 整合mybatis --> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean" > <!-- 關聯數據源 --> <property name="dataSource" ref="dataSource"/> <!-- 關聯mybatis的配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 添加別名設置 --> <property name="typeAliasesPackage" value="com.sxt.bean"/> </bean> <!-- 配置掃描的路徑 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name="basePackage" value="com.sxt.mapper"/> </bean> </beans>
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8 names=root password=1234
package com.sxt.bean; public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; } }
public interface UserMapper { public List<User> query(); }
<?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.sxt.mapper.UserMapper"> <select id="query" resultType="user"> select * from t_user </select> </mapper>
/** * 接口 * @author Administrator * */ public interface IUserService { public List<User> query(); }
/** * 實現類 * @author Administrator * */ @Service public class UserServiceImpl implements IUserService { @Resource //獲取代理對象 session.getMapper(UserMapper.class); private UserMapper mapper; @Override public List<User> query() { // TODO Auto-generated method stub return mapper.query(); }
public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); IUserService service = ac.getBean(IUserService.class); List<User> list = service.query(); for (User user : list) { System.out.println(user); } }