eclipse:Version: 2019-03 (4.11.0)
jdk:jdk-12.0.1
maven:apache-maven-3.6.2
打开eclipse->new->other->maven project
<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.0modelVersion>
<groupId>com.nys.ms1groupId>
<artifactId>mybatis_spring0artifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<spring_version>4.3.2.RELEASEspring_version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring_version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.8version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>javax.annotationgroupId>
<artifactId>jsr250-apiartifactId>
<version>1.0version>
dependency>
dependencies>
project>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/nys
jdbc.username=root
jdbc.password=nys123
log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
-->>create database nys;
-->>use nys;
-->>create table users(
uid int primary key auto_increment,
uname varchar(10) not null,
uage int not null
)
-->>insert into users(uname,uage) values
("nys",21),
("zs",22),
("tmh",21),
("llj",22),
("zbs",23),
("tml",20),
("ljy",20)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="service">context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="pojo" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper" />
bean>
beans>
package pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class Users {
@Autowired //自动创建get,set方法
private Integer uid;
@Autowired //自动创建get,set方法
private String uname;
@Autowired //自动创建get,set方法
private Integer uage;
@Override
public String toString() {
return "Users [uid=" + uid + ", uname=" + uname + ", uage=" + uage + "]";
}
}
package mapper;
import pojo.Users;
public interface UsersMapper {
Users selectByUname(String uname);
}
package service;
import pojo.Users;
public interface UsersService {
Users getUsersByUname(String uname);
}
package service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import mapper.UsersMapper;
import pojo.Users;
import service.UsersService;
@Service("usersService")
public class UsersServiceImpl implements UsersService {
@Resource
private UsersMapper usersMapper;
public Users getUsersByUname(String uname) {
Users users=usersMapper.selectByUname(uname);
return users;
}
}
注:由于maven项目中,src/main/java目录下的非.java文件均不会被编译到target中,所以这里如果将UsersMapper.xml文件创建到UsersMapper.java同位置,之后的test中将会报错"Invalid bound statement (not found)"。详情:Eclipse报错:Invalid bound statement (not found)
按照maven的结构,我们应该将UsersMapper.xml文件创建于java/main/resources/下,并创建一系列与UsersMapper.java包同名的文件夹来存放该xml文件
<mapper namespace="mapper.UsersMapper">
<select id="selectByUname" parameterType="java.lang.String" resultType="Users">
select * from users where uname = #{uname}
select>
mapper>
package mapper;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pojo.Users;
import service.UsersService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:mybatis_spring.xml")
public class UsersMapperTest {
@Resource
private UsersService usersService;
@Test
public void testSelectByid() {
Users users=usersService.getUsersByUname("nys");
System.out.println(users);
}
}