A, xml configuration version
1. Import dependence
<-! MyBatis and Spring integration package provided by MyBatis -> < dependency > < the groupId > org.mybatis </ the groupId > < the artifactId > MyBatis-Spring </ the artifactId > < Version > 1.3.0 </ Version > < / dependency > <-! MyBatis core jar file -> < dependency > < groupId > org.mybatis </ groupId > < artifactId >mybatis</artifactId> <version>3.4.1</version> </dependency>
2. Create entity classes
public class User { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
3. Create Dao layer interface and its mapping file
public interface UserDao { List<User> getAll(); }
<?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"> <!--小配置根节点 namespace代表命名空间--> <mapper namespace="com.wdksoft.dao.UserDao"> <select id="getAll" resultType="User"> select * from user </select> </mapper>
Layer interface and its implementation class 4.Service
public interface UserService { List<User> getAll(); }
public class UserServiceImpl implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public List<User> getAll() { return userDao.getAll(); } }
5. Profiles
<? Xml Version = "1.0" encoding = "UTF-8" ?> <-! Header file xml file, is binding on the file (for example: which nodes must be present) -> ! < DOCTYPE the Configuration the PUBLIC "- // 3.0 // EN mybatis.org//DTD Config" "http://mybatis.org/dtd/mybatis-3-config.dtd" > < Configuration > < typeAliases > < Package name = "com.wdksoft .entity " /> </ typeAliases > < mappers > <-! map file manage our configuration -> < Package Penalty for name =" com.wdksoft.dao"/> </mappers> </configuration>
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/bank?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=123
<!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </ ->Scanning Dao layer, omitting the code<-!>The bean <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wdksoft.dao"/> </bean> <!--注入dao层--> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.wdksoft.dao.UserDao"></property> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property> </bean> <!--注入Service--> <bean id="userService" class="com.wdksoft.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
Second, the edition notes
Spring configuration file:
<!--包扫描器--> <context:component-scan base-package="com.wdksoft"/> <!--加载配置文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置SqlSessionFactoryBean--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--扫描Dao层,省略代码--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wdksoft.dao"/> </bean>
Other add annotations corresponding to