• spring Mybatis集成


    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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yujian</groupId>
    <artifactId>spring-mybatis-demo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.3.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.1.3.RELEASE</version>
    </dependency>
    <!--spring连接jdbc-->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.3.RELEASE</version>
    </dependency>
    <!--jdbc驱动-->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
    </dependency>
    <!--c3p0数据源,替代DBHelper的-->
    <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
    </dependency>
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <skip>true</skip>
    </configuration>
    </plugin>
    </plugins>
    <resources>
    <resource>
    <directory>src/main/resources</directory>
    <includes>
    <include>**/*</include>
    </includes>
    </resource>
    <resource>
    <directory>src/main/java</directory>
    <includes>
    <include>**/*.xml</include>
    </includes>
    </resource>
    </resources>
    </build>

    </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-context.xsd">
    <!--<context:property-placeholder location="db.properties"/>
    <context:component-scan base-package="com.yujian"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="password" value="${dbPass}"/>
    <property name="user" value="${dbName}"/>
    <property name="jdbcUrl" value="${driverUrl}"/>
    <property name="driverClass" value="${driverClass}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    &lt;!&ndash;<property name="mapperLocations" value="classpath*:com/yujian/dao/*Mapper.xml"/>&ndash;&gt;
    &lt;!&ndash;<property name="typeAliasesPackage" value="com.yujian.model"/>&ndash;&gt;
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="basePackage" value="com.yujian.dao"/>
    </bean>-->

    <!--<bean id="studentService" class="com.yujian.service.StudentService">
    <property name="studentMapper" ref="studentMapper"/>
    </bean>-->
    </beans>
    db.properties
    driverClass=com.mysql.jdbc.Driver
    driverUrl=jdbc:mysql://localhost:3306/school?useSSL=false
    dbName=root
    dbPass=root
    全注解方式
    
    
    package com.yujian.config;

    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.jdbc.core.JdbcTemplate;

    import java.beans.PropertyVetoException;

    @Configuration
    @MapperScan("com.yujian.dao")
    @ComponentScan("com.yujian")
    @PropertySource("classpath:db.properties")
    public class MyConfig {
    @Value("${driverClass}")
    private String driverClass;
    @Value("${driverUrl}")
    private String driverUrl;
    @Value("${dbName}")
    private String dbName;
    @Value("${dbPass}")
    private String dbPass;
    @Bean
    public ComboPooledDataSource dataSource(){
    ComboPooledDataSource dataSource=new ComboPooledDataSource();
    try {
    dataSource.setDriverClass(driverClass);
    dataSource.setJdbcUrl(driverUrl);
    dataSource.setUser(dbName);
    dataSource.setPassword(dbPass);
    } catch (PropertyVetoException e) {
    e.printStackTrace();
    }
    return dataSource;
    }
    /*@Bean
    public JdbcTemplate jdbcTemplate(){
    JdbcTemplate jdbcTemplate=new JdbcTemplate();
    jdbcTemplate.setDataSource(dataSource());
    return jdbcTemplate;
    }*/
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(){
    SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource());
    return sqlSessionFactoryBean;
    }


    }
    启动文件
    package com.yujian;

    import com.yujian.config.MyConfig;
    import com.yujian.model.Student;
    import com.yujian.service.StudentService;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import java.util.List;


    public class MainApp {
    public static void main(String[] args) {
    // ApplicationContext ctx=new ClassPathXmlApplicationContext("app*.xml");//这是配置文件,下面为全注解
    ApplicationContext ctx=new AnnotationConfigApplicationContext(MyConfig.class);
    StudentService studentService=(StudentService) ctx.getBean("studentService");
    List<Student> students=studentService.findStudentList();
    System.out.println(students);
    }
    }














     
  • 相关阅读:
    动态、指针field-symbols初探
    简单的OO ALV显示ALV及下载
    python运算符号
    linux ubuntu 学习总结(day01)基本命令学习
    Linux之Ubuntu基本命令提炼,分条列出
    linux常用基本命令
    EMC光纤交换机故障处理和命令分析
    Java求一个数组中的最大值和最小值
    【SSH网上商城项目实战30】项目总结
    【SSH网上商城项目实战29】使用JsChart技术在后台显示商品销售报表
  • 原文地址:https://www.cnblogs.com/pursue339/p/10615179.html
Copyright © 2020-2023  润新知