首页 > 编程语言 > 详细

08MP-Spring+Mybatis+MP实现查询

时间:2021-09-21 00:59:32      阅读:8      评论:0      收藏:0      [点我收藏+]

引入了Spring框架,数据源、构建等工作就交给了Spring管理。
1、创建子Module,引入相关的依赖

<!--引入spring的相关配置文件-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>

2、编写db.properties,并引入log4j.properties的日志配置文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mp
jdbc.username=root
jdbc.password=2000820.
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n

3、编写application Context.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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载配置文件-->
    <context:property-placeholder location="classpath:*.properties"></context:property-placeholder>

    <!--定义数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <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>
        <property name="maxActive" value="10"></property>
        <property name="minIdle" value="5"></property>
    </bean>
    
    <!--这里使用MP提供的sqlSessionFactory,完成spring与MP的整合-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!--扫描mapper接口,使用的依然是mybatis原生的扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.study.mapper"></property>    **//注意:这里是指定的包,不是具体到接口**
    </bean>
</beans>

4、编写User和UserMapper接口

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_user")
public class User {
    private int id;
    private String user_name;
    private String password;
    private String name;
    private int age;
    private String email;
}
public interface UserMapper extends BaseMapper<User> {
    List<User> findAll();
}

5、编写测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MyBatisTest {

@Autowired
private UserMapper userMapper;

@Test
public void test(){
    //查询条件为空,即查询所有
    List<User> list = userMapper.selectList(null);

    for (User user : list) {
        System.out.println(user);
    }
}

}

6、会出错。因为是在Test文件夹下编写的测试文件,检测不到我们的db.properties
解决方法1:将applicationContext.xml中的url、driver等直接写出来,不要引用
解决方法2:Test下创建resources文件夹,将db.properties和applicationContext.xml文件引入即可

08MP-Spring+Mybatis+MP实现查询

原文:https://www.cnblogs.com/morehair/p/15306105.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!