1导包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--下面是spring的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.4</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
</dependencies>
2spring-mybatis.xml
<!--开启组建扫描-->
<context:component-scan base-package="service所在的包"/>
<!--加载properties配置文件-->
<context:property-placeholder location="数据库配置文件"/>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置SqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--扫描mapper文件-->
<property name="mapperLocations" value="mapper xml文件所在目录"/>
<!--配置别名,可有可无-->
<property name="typeAliasesPackage" value="实体类所在目录"/>
<!--加载配置文件,可有可无-->
<!--<property name="configLocation" value="mybatis配置文件"/>-->
</bean>
<!--配置MapperScannerConfigurer:用来扫描mapper接口,并且创建接口的实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--扫描某个包下的接口-->
<property name="basePackage" value="实体类包名"/>
</bean>
3dao层
public interface UserDao {
public User selectUserById(Integer uid);
}
service层
@Service //创建对象
public class UserServiceImpl implements IUserService {
@Autowired //注入userdao的实现类
private UserDao userDao;
public User selectUserById(Integer uid) {
return userDao.selectUserById(uid);
}
}
5、测试类
public void testSelectUserById(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-mybatis.xml");
IUserService service = context.getBean(UserServiceImpl.class);
User user = service.selectUserById(3);
System.out.println(user);
}
网友评论