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

时间:2021-05-09 06:26:04

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

 <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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MybatisTest</groupId>
<artifactId>MybatisTest-Demo1</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>MybatisTest-Demo1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!--mysql driver-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency> <!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.1.RELEASE</version>
</dependency> <!--Spring Web + Spring MVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency> <!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency> <!--servlet/jsp api start-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency> <!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency> <!--aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.6</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.6</version>
</dependency> <!--jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.5.RELEASE</version>
</dependency> <!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatis spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency> <!--spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency> </dependencies>
<build>
<finalName>MybatisTest-Demo1</finalName> <plugins>
<!--servlet容器 jetty插件-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.10.v20160621</version>
</plugin> <!--mybatis 逆向工程插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins> </build>
</project>

2 model:Employee.java

 package com.mybatis.model;

 /**
* Created by wanggenshen_sx on 2016/12/23.
*/
public class Employee { private int id;
private String lastName;
private String email; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email+" ]" ;
}
}

3  mapper:EmployeeMapper.java

 package com.mybatis.mapper;

 import com.mybatis.model.Employee;
import org.apache.ibatis.annotations.Select; import java.util.List; /**
* Created by wanggenshen_sx on 2016/12/26.
*/ public interface EmployeeMapper {
@Select("select id,lastName,email from employee where id=#{id}")
Employee getUserById(int id); @Select("select * from employee")
List<Employee> getAllUsers();
}

4 测试类

 package com.mybatis.test;

 import com.mybatis.mapper.EmployeeMapper;
import com.mybatis.model.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* Created by wanggenshen_sx on 2016/12/26.
*/
@RunWith(SpringJUnit4ClassRunner.class)//使用Springtest框架
@ContextConfiguration(locations={"classpath:spring-mybatis.xml"})//加载配置
public class EmployeeTest { @Autowired //注入
private EmployeeMapper employeeMapper;
/**
* 根据一个id得到一条用户信息
*/
/*@Test
public void testGetUser(){
SqlSession session = MybatisUtils.getSqlSessionFactory().openSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); Employee employee = mapper.getUserById(1);
System.out.println("*"+employee);
session.close();
}*/ @Test
public void testGetUser(){
Employee employee = employeeMapper.getUserById(1);
System.out.println(employee);
} /**
* 得到所有用户信息
*/
@Test
public void testGetAllUsers(){
List<Employee> employees = employeeMapper.getAllUsers();
System.out.println(employees);
}
}

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

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

 <?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"/>
<property name="username" value="root"/>
<property name="password" value="920614"/>
</bean> <!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource / typeAliasesPackage
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="typeAliasesPackage" value="com.mybatis.model"/>
</bean> <!--
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
-->
<bean id="config" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mybatis.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> <!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"/>
</bean> <!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="manager" /> <bean class="com.mybatis.test.EmployeeTest" />
</beans>

6 结构:

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