• Intellij IDEA +MAVEN+Jetty实现Spring整合Mybatis


    1 pom.xml(这里出现transaction错误,是版本的问题)

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3   <modelVersion>4.0.0</modelVersion>
      4   <groupId>MybatisTest</groupId>
      5   <artifactId>MybatisTest-Demo1</artifactId>
      6   <packaging>war</packaging>
      7   <version>1.0-SNAPSHOT</version>
      8   <name>MybatisTest-Demo1 Maven Webapp</name>
      9   <url>http://maven.apache.org</url>
     10   <dependencies>
     11     <dependency>
     12       <groupId>junit</groupId>
     13       <artifactId>junit</artifactId>
     14       <version>3.8.1</version>
     15       <scope>test</scope>
     16     </dependency>
     17 
     18     <!--mysql driver-->
     19     <dependency>
     20       <groupId>mysql</groupId>
     21       <artifactId>mysql-connector-java</artifactId>
     22       <version>5.1.6</version>
     23     </dependency>
     24 
     25     <!--spring-->
     26     <dependency>
     27       <groupId>org.springframework</groupId>
     28       <artifactId>spring-beans</artifactId>
     29       <version>4.3.1.RELEASE</version>
     30     </dependency>
     31 
     32     <dependency>
     33       <groupId>org.springframework</groupId>
     34       <artifactId>spring-core</artifactId>
     35       <version>4.3.1.RELEASE</version>
     36     </dependency>
     37 
     38     <dependency>
     39       <groupId>org.springframework</groupId>
     40       <artifactId>spring-context</artifactId>
     41       <version>4.3.1.RELEASE</version>
     42     </dependency>
     43 
     44     <!--Spring Web + Spring MVC-->
     45     <dependency>
     46       <groupId>org.springframework</groupId>
     47       <artifactId>spring-web</artifactId>
     48       <version>4.3.1.RELEASE</version>
     49     </dependency>
     50     <dependency>
     51       <groupId>org.springframework</groupId>
     52       <artifactId>spring-webmvc</artifactId>
     53       <version>4.3.1.RELEASE</version>
     54     </dependency>
     55 
     56     <!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
     57     <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
     58     <dependency>
     59       <groupId>javax.servlet</groupId>
     60       <artifactId>jstl</artifactId>
     61       <version>1.2</version>
     62     </dependency>
     63 
     64     <dependency>
     65       <groupId>taglibs</groupId>
     66       <artifactId>standard</artifactId>
     67       <version>1.1.2</version>
     68     </dependency>
     69 
     70     <!--servlet/jsp api start-->
     71     <dependency>
     72       <groupId>javax.servlet</groupId>
     73       <artifactId>servlet-api</artifactId>
     74       <version>2.5</version>
     75     </dependency>
     76 
     77     <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
     78     <dependency>
     79       <groupId>javax.servlet.jsp</groupId>
     80       <artifactId>jsp-api</artifactId>
     81       <version>2.2</version>
     82     </dependency>
     83 
     84     <!--c3p0-->
     85     <dependency>
     86       <groupId>com.mchange</groupId>
     87       <artifactId>c3p0</artifactId>
     88       <version>0.9.5.1</version>
     89     </dependency>
     90 
     91     <!--aspectj-->
     92     <dependency>
     93       <groupId>org.aspectj</groupId>
     94       <artifactId>aspectjweaver</artifactId>
     95       <version>1.8.6</version>
     96     </dependency>
     97 
     98     <dependency>
     99       <groupId>org.aspectj</groupId>
    100       <artifactId>aspectjrt</artifactId>
    101       <version>1.8.6</version>
    102     </dependency>
    103 
    104     <!--jdbc-->
    105     <dependency>
    106       <groupId>org.springframework</groupId>
    107       <artifactId>spring-jdbc</artifactId>
    108       <version>3.0.5.RELEASE</version>
    109     </dependency>
    110 
    111     <!--mybatis-->
    112     <dependency>
    113       <groupId>org.mybatis</groupId>
    114       <artifactId>mybatis</artifactId>
    115       <version>3.4.1</version>
    116     </dependency>
    117     <!--mybatis spring整合-->
    118     <dependency>
    119       <groupId>org.mybatis</groupId>
    120       <artifactId>mybatis-spring</artifactId>
    121       <version>1.3.0</version>
    122     </dependency>
    123     <dependency>
    124       <groupId>junit</groupId>
    125       <artifactId>junit</artifactId>
    126       <version>RELEASE</version>
    127     </dependency>
    128 
    129   <!--spring-test-->
    130     <dependency>
    131       <groupId>org.springframework</groupId>
    132       <artifactId>spring-test</artifactId>
    133       <version>3.2.3.RELEASE</version>
    134       <scope>test</scope>
    135     </dependency>
    136     <dependency>
    137       <groupId>org.springframework</groupId>
    138       <artifactId>spring-test</artifactId>
    139       <version>RELEASE</version>
    140     </dependency>
    141 
    142   </dependencies>
    143   <build>
    144     <finalName>MybatisTest-Demo1</finalName>
    145 
    146     <plugins>
    147       <!--servlet容器 jetty插件-->
    148       <plugin>
    149         <groupId>org.eclipse.jetty</groupId>
    150         <artifactId>jetty-maven-plugin</artifactId>
    151         <version>9.3.10.v20160621</version>
    152       </plugin>
    153 
    154       <!--mybatis 逆向工程插件-->
    155       <plugin>
    156         <groupId>org.mybatis.generator</groupId>
    157         <artifactId>mybatis-generator-maven-plugin</artifactId>
    158         <version>1.3.2</version>
    159         <configuration>
    160           <verbose>true</verbose>
    161           <overwrite>true</overwrite>
    162         </configuration>
    163       </plugin>
    164       <plugin>
    165         <groupId>org.apache.maven.plugins</groupId>
    166         <artifactId>maven-compiler-plugin</artifactId>
    167         <configuration>
    168           <source>1.7</source>
    169           <target>1.7</target>
    170         </configuration>
    171       </plugin>
    172     </plugins>
    173 
    174   </build>
    175 </project>

    2 model:Employee.java

     1 package com.mybatis.model;
     2 
     3 /**
     4  * Created by wanggenshen_sx on 2016/12/23.
     5  */
     6 public class Employee {
     7 
     8     private int id;
     9     private String lastName;
    10     private String email;
    11 
    12     public int getId() {
    13         return id;
    14     }
    15     public void setId(int id) {
    16         this.id = id;
    17     }
    18     public String getLastName() {
    19         return lastName;
    20     }
    21     public void setLastName(String lastName) {
    22         this.lastName = lastName;
    23     }
    24     public String getEmail() {
    25         return email;
    26     }
    27     public void setEmail(String email) {
    28         this.email = email;
    29     }
    30     @Override
    31     public String toString() {
    32         return "Employee [id=" + id + ", lastName=" + lastName + ", email="
    33                 + email+" ]" ;
    34     }
    35 }

    3  mapper:EmployeeMapper.java

     1 package com.mybatis.mapper;
     2 
     3 import com.mybatis.model.Employee;
     4 import org.apache.ibatis.annotations.Select;
     5 
     6 import java.util.List;
     7 
     8 /**
     9  * Created by wanggenshen_sx on 2016/12/26.
    10  */
    11 
    12 public interface EmployeeMapper {
    13     @Select("select id,lastName,email from employee where id=#{id}")
    14     Employee getUserById(int id);
    15 
    16     @Select("select * from employee")
    17     List<Employee> getAllUsers();
    18 }

    4 测试类

     1 package com.mybatis.test;
     2 
     3 import com.mybatis.mapper.EmployeeMapper;
     4 import com.mybatis.model.Employee;
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.test.context.ContextConfiguration;
     9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10 
    11 import java.util.List;
    12 
    13 /**
    14  * Created by wanggenshen_sx on 2016/12/26.
    15  */
    16 @RunWith(SpringJUnit4ClassRunner.class)//使用Springtest框架
    17 @ContextConfiguration(locations={"classpath:spring-mybatis.xml"})//加载配置
    18 public class EmployeeTest {
    19 
    20 
    21     @Autowired  //注入
    22     private EmployeeMapper employeeMapper;
    23     /**
    24      * 根据一个id得到一条用户信息
    25      */
    26     /*@Test
    27     public void testGetUser(){
    28         SqlSession session = MybatisUtils.getSqlSessionFactory().openSession();
    29         EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
    30 
    31         Employee employee = mapper.getUserById(1);
    32         System.out.println("*"+employee);
    33         session.close();
    34     }*/
    35 
    36     @Test
    37     public void testGetUser(){
    38         Employee employee = employeeMapper.getUserById(1);
    39         System.out.println(employee);
    40     }
    41 
    42     /**
    43      * 得到所有用户信息
    44      */
    45     @Test
    46     public void testGetAllUsers(){
    47         List<Employee> employees = employeeMapper.getAllUsers();
    48         System.out.println(employees);
    49     }
    50 }

    5 配置文件:spring-mybatis.xml(

    sqlSessionFactoryBeanName与以前不一样
    第3个步骤此处就是直接扫描包,而不用再在mybatis.xml中去注册EmployeeMapper接口

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:tx="http://www.springframework.org/schema/tx"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
     7 
     8     <!-- 1. 数据源 : DriverManagerDataSource -->
     9     <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    10         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    11         <property name="url" value="jdbc:mysql://localhost:3306/mytest"/>
    12         <property name="username" value="root"/>
    13         <property name="password" value="920614"/>
    14     </bean>
    15 
    16     <!--
    17         2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
    18             dataSource / typeAliasesPackage
    19     -->
    20     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    21         <property name="dataSource" ref="datasource"/>
    22         <property name="typeAliasesPackage" value="com.mybatis.model"/>
    23     </bean>
    24 
    25     <!--
    26         3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
    27             sqlSessionFactory / basePackage
    28     -->
    29     <bean id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    30         <property name="basePackage" value="com.mybatis.mapper"/>
    31         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    32     </bean>
    33 
    34     <!-- 4. 事务管理 : DataSourceTransactionManager -->
    35     <bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    36         <property name="dataSource" ref="datasource"/>
    37     </bean>
    38 
    39     <!-- 5. 使用声明式事务 -->
    40     <tx:annotation-driven transaction-manager="manager" />
    41 
    42     <bean class="com.mybatis.test.EmployeeTest" />
    43 </beans>

    6 结构:

  • 相关阅读:
    linux内核中的crng是什么?
    linux内核中的hisi_sas是什么?
    linux内核中的brd是什么?
    内核中的xenfs是什么?
    linux内核中的cfq输入输出调度算法
    linux内核中的最简单的输入输出调度算法noop
    linux内核中侧async_tx是什么?
    linux内核中的fuse是什么?
    Executors.newSingleThreadScheduledExecutor();线程池中放入多个线程问题
    Executors.newSingleThreadScheduledExecutor() 问题
  • 原文地址:https://www.cnblogs.com/noaman/p/6223059.html
Copyright © 2020-2023  润新知