Spring整合Mybatis-Plus
作者:互联网
Spring整合Mybatis-Plus
实现查询User
-
编写jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///mp jdbc.username=root jdbc.password=root
-
pom依赖
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>Wsy-Mybatis-Plus</artifactId> <groupId>com.yun</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Wsy-Mybatis-Plus-Spring</artifactId> <properties> <spring.version>5.1.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
-
编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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" 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-beans.xsd"> <!--引入properties文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--数据源配置--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassLoader" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--1,将sqlSessionFactory对象的创建交给spring--> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!--2,mapper映射扫描--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yun.mapper"/> </bean> </beans>
-
编写User对象以及UserMapper接口
@Data @NoArgsConstructor @AllArgsConstructor @TableName("user") public class User { /** id */ private Long id; /** name */ private String name; /** age */ private Integer age; /** email */ private String email; }
public interface UserMapper extends BaseMapper<User> { /** * 查询所有用户 * @return */ List<User> getAll(); }
-
编写测试用例
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringTest { @Autowired private UserMapper mapper; @Test public void test(){ List<User> users = mapper.selectList(null); users.forEach(item -> { System.out.println(item); }); } }
运行结果
User(id=1, name=Jone, age=18, email=test1@baomidou.com) User(id=2, name=Jack, age=20, email=test2@baomidou.com) User(id=3, name=Tom, age=28, email=test3@baomidou.com) User(id=4, name=Sandy, age=21, email=test4@baomidou.com) User(id=5, name=Billie, age=24, email=test5@baomidou.com)
标签:jdbc,name,Spring,id,Plus,User,Mybatis,com,email 来源: https://www.cnblogs.com/wangshaoyun/p/16445820.html