1.创建gradle项目:File->New->Project
1.添加依赖
//spring依赖
compile 'org.springframework:spring-webmvc:5.1.1.RELEASE'
compile 'org.springframework:spring-beans:5.1.1.RELEASE'
compile 'org.springframework:spring-context:5.1.1.RELEASE'
compile 'org.springframework:spring-context-support:5.1.1.RELEASE'
compile 'org.springframework:spring-web:5.1.1.RELEASE'
compile 'org.springframework:spring-tx:5.1.1.RELEASE'
compile 'org.springframework:spring-jdbc:5.1.1.RELEASE'
compile 'org.springframework:spring-test:5.1.1.RELEASE'
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.controller">context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
bean>
beans>
web.xml文件内容:里面涉及到的配置文件将在稍后进行介绍添加。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springConfig/spring-mybatis.xmlparam-value>
context-param>
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springConfig/applicationContext.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
<async-supported>trueasync-supported>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
3.添加全局spring配置文件
applicationContext.xml
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="org.controller">context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
bean>
beans>
4.在web.xml里配置,主要是以下内容,位置在图片里标注了
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springConfig/applicationContext.xmlparam-value>
init-param>
1 .添加配置文件
# MySQL 驱动
jdbc.driverClassName = com.mysql.jdbc.Driver
# JDBC URL
jdbc.url =jdbc:mysql://127.0.0.1:3306/myproject?useUnicode:true&characterEncoding:UTF-8
# 数据库用户名及密码
jdbc.username = root
jdbc.password = 123456
# 实体类所在包
package.model = org.mybatis.entity
# mapper 所在包
package.mapper = org.mybatis.mapper
# mapper xml 文件所在包 默认存储在 resources 目录下
package.xml = mybatis/mapping
# 初始化连接大小
initialSize=0
# 连接池最大数量
maxActive=20
# 连接池最大空闲
maxIdle=20
# 连接池最小空闲
minIdle=1
# 获取连接最大等待时间
maxWait=60000
<generatorConfiguration>
<context id="default" targetRuntime="MyBatis3Simple" defaultModelType="flat" >
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">plugin>
<commentGenerator>
<property name="suppressAllComments" value="true">property>
<property name="suppressDate" value="true">property>
<property name="javaFileEncoding" value="utf-8"/>
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter">property>
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
commentGenerator>
<jdbcConnection driverClass="${driverClass}"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}">
<property name="enableSubPackages" value="true">property>
<property name="trimStrings" value="true">property>
javaModelGenerator>
<sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}">
<property name="enableSubPackages" value="true">property>
sqlMapGenerator>
<javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="%">
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
table>
context>
generatorConfiguration>
2.添加依赖
configurations {
mybatisGenerator
}
dependencies{
// mybatis-generator mybatis逆向
mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.7'
mybatisGenerator 'mysql:mysql-connector-java:5.1.45'
mybatisGenerator 'tk.mybatis:mapper:4.0.4'
}
def getDbProperties = {
def properties = new Properties()
file("src/main/resources/myBatisGenerator/jdbc_config.properties").withInputStream { inputStream ->
properties.load(inputStream)
}
properties
}
task mybatisGenerator << {
def properties = getDbProperties()
ant.properties['targetProject'] = projectDir.path
ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
ant.properties['userId'] = properties.getProperty("jdbc.username")
ant.properties['password'] = properties.getProperty("jdbc.password")
ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
ant.properties['modelPackage'] = properties.getProperty("package.model")
ant.properties['mapperPackage'] = properties.getProperty("package.mapper")
ant.properties['sqlMapperPackage'] = properties.getProperty("package.xml")
ant.taskdef(
name: 'mbgenerator',
classname: 'org.mybatis.generator.ant.GeneratorAntTask',
classpath: configurations.mybatisGenerator.asPath
)
ant.mbgenerator(overwrite: true,
configfile: 'src/main/resources/myBatisGenerator/generatorConfig.xml', verbose: true) {
propertyset {
propertyref(name: 'targetProject')
propertyref(name: 'userId')
propertyref(name: 'driverClass')
propertyref(name: 'connectionURL')
propertyref(name: 'password')
propertyref(name: 'src_main_java')
propertyref(name: 'src_main_resources')
propertyref(name: 'modelPackage')
propertyref(name: 'mapperPackage')
propertyref(name: 'sqlMapperPackage')
}
}
}
1.添加依赖
dependencies{
//mybatis依赖
compile 'org.mybatis:mybatis:3.4.6'
//mysql依赖
compile 'mysql:mysql-connector-java:5.1.45'
//mybatis-spring整合
compile 'org.mybatis:mybatis-spring:1.3.2'
}
2.resource目录下添加配置文件spring-mybatis.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="org.mybatis.mapper" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myBatisGenerator/jdbc_config.properties" />
bean>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${initialSize}">property>
<property name="maxActive" value="${maxActive}">property>
<property name="minIdle" value="${minIdle}">property>
<property name="maxWait" value="${maxWait}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/mapping/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
beans>
3.web.xm里配置读取配置
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springConfig/spring-mybatis.xmlparam-value>
context-param>
/*
Created by IntelliJ IDEA.
User: Long
Date: 2018/10/24
Time: 11:30
To change this template use File | Settings | File Templates.
*/
package org.controller;
import org.mybatis.entity.User;
import org.mybatis.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value = "/insert", produces={"application/json; charset=UTF-8"})
@ResponseBody
public String insertUser() {
User user = new User();
user.setEmail("sssssasas");
user.setPassword("44555");
user.setSex("sss");
userMapper.insert(user);
return user.toString();
}
@RequestMapping(value = "/findAll",produces={"application/json; charset=UTF-8"})
public List<User> findAll() {
return userMapper.selectAll();
}
}
compile 'org.mybatis:mybatis-spring:1.3.2'
会出现错误,在 测试时会出现:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
-解决方式:
引入依赖,并正确配置spring-mybatis.xml文件
spring注解解析