package test.learn.dao; import org.apache.ibatis.annotations.Param; import org.mybatis.spring.annotation.MapperScan; import test.learn.pojo.User; import test.learn.pojo.UserExample; import java.util.List; @MapperScan public interface UserMapper { int countByExample(UserExample example); int deleteByExample(UserExample example); int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); List<User> selectByExample(UserExample example); User selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example); int updateByExample(@Param("record") User record, @Param("example") UserExample example); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
package test.learn.service; import org.springframework.stereotype.Service; import test.learn.dao.UserMapper; import test.learn.pojo.User; import javax.annotation.Resource; @Service public class UserServiceImpl implements UserService { @Resource private UserMapper userDao; public User get(Integer id) { return userDao.selectByPrimaryKey(id); } }
![](https://i-blog.csdnimg.cn/blog_migrate/ce8841ef4143a86ac1b30bc72bd4b3b0.png)
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/yftest?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false jdbc.username=root jdbc.password=123456
<?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="false"/> <!-- 设置mybatis内置日志工厂 --> <setting name="logImpl" value="LOG4J"/> <!-- 支持Null值(value)输出Key --> <setting name="callSettersOnNulls" value="true"/> </settings> </configuration>
<?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.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:/mybatis/db.properties"/> </bean> <!-- dataSource配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="filters" value="log4j"/> <property name="maxActive" value="5"/> <property name="initialSize" value="1"/> <property name="maxWait" value="6000"/> </bean> <!-- mybatis文件配置,mapper.xml文件扫描 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:/mybatis/mybatis.xml"/> <property name="mapperLocations" value="classpath:/mybatis/mapper/*Mapper.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- mapper注解扫描器配置,扫描@MapperScan注解,自动生成代码对象 --> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="test.learn.dao"/> <property name="sqlSessionFactoryBeanName" value="sessionFactory"/> </bean> <bean id="userService" class="test.learn.service.UserServiceImpl"/> </beans>
@Test public void myTest2(){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserService userService=(UserService)context.getBean("userService"); System.out.println(userService.get(1)); }
记得加注解
Mapper类,实体类和单表的xml的是用MyBatis自带的generate生成的