断断续续摸索两周,把环境搭建起来了,在这记录一下
1.项目结构图
2添加maven依赖
<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>cn.com.xie</groupId>
<artifactId>Spring-SpringMvc-mybatis</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-SpringMvc-mybatis Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build-sourceEncoding>UTF-8</project.build-sourceEncoding>
<project.reporting-outputEncoding>UTF-8</project.reporting-outputEncoding>
<spring.version>4.3.5.RELEASE</spring.version>
<mybatis.version>3.4.1</mybatis.version>
<tk.mybatis.mapper.version>3.3.8</tk.mybatis.mapper.version>
<javaee.version>7.0</javaee.version>
<net.sf.json>2.4</net.sf.json>
<mysql.version>5.1.41</mysql.version>
<pagehelper.varsion>4.1.0</pagehelper.varsion>
<mybatis.spring.version>1.3.1</mybatis.spring.version>
<commons-dbcp.version>1.2.2</commons-dbcp.version>
<aspectj.version>1.8.6</aspectj.version>
<jackson.version>2.9.0</jackson.version>
<validator.version>5.2.4.Final</validator.version>
</properties>
<dependencies>
<!--java ee 依赖 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee.version}</version>
</dependency>
<!--json 依赖 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier>
<version>${net.sf.json}</version>
</dependency>
<!--mysql 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!--数据库连接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<!--mybatis 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--mybatis 分页查询插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.varsion}</version>
</dependency>
<!--整合mybatis和spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>${tk.mybatis.mapper.version}</version>
</dependency>
<!--spring 依赖 -->
<!--底层核心-提供IOC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<!-- bean validate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${validator.version}</version>
</dependency>
<!--jackson 依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- log4j日志配置 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>spring-springmvc-mybatis</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.配置数据库连接信息jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssmtest?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=xgr1234
4.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--编码过滤器配置 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springMVC DispatcherServlet 前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<!--在启动时加载 -->
<load-on-startup>1</load-on-startup>
<!--异步 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
6.配置spring-mvc.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"
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-3.0.xsd">
<!--扫描Controller -->
<context:component-scan base-package="com.xie.controller" />
<!--开启springMVC注解模式 -->
<mvc:annotation-driven />
<!--静态资源默认servlet配置 -->
<mvc:default-servlet-handler />
<!--视图配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".html" />
</bean>
</beans>
7.配置mybatis-config.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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--mybatis整合spring的配置 -->
<!--扫包(service注解) -->
<context:component-scan base-package="com.xie.service" />
<!--jdbc链接数据 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--数据库链接配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--sqlSessionFactory配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!--扫描mapper对应配置文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/*.xml" />
<!--mybatis-config日志输出-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
</bean>
<!--扫描mapper类型 -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!--扫描mapper包 -->
<property name="basePackage" value="com.xie.dao" />
</bean>
<!--配置事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据库链接池 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!--基于注解申明式事务配置 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
8.配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
</configuration>
9.mapper数据库映射文件(TestMapper.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xie.dao.Mapperuser">
<!--namespace :指向extends Mapper的dao层 -->
<!--
参数注解
id:唯一标识,在service中调取查询的依据
resultType:查询结果返回的指向
parameterType:需要传的参数指向的实体
-->
<select id="queryTestInfo" resultType="com.xie.entity.Testuser">
SELECT * FROM user
</select>
<!--
参数注解
parameterType:需要传的参数指向的实体
由于新增数据返回的是一个布尔类型或者是一个int类型,不需要指定返回指向resultType
-->
<insert id="adduser" parameterType="com.xie.entity.Testuser">
insert into user(ID,username,password,address)
values(#{id},#{username},#{password},#{address})
</insert>
</mapper>
10.数据库建表
11.测试类
Controller
package com.xie.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xie.entity.Testuser;
import com.xie.service.ServiceTest;
/*
* controller 控制器
*
* */
@Controller
@RequestMapping("api/test")
public class ControllerTest {
@Autowired
private ServiceTest servicetest;
/*
* 查询所有用户信息
*
* */
@RequestMapping("/getUser")
@ResponseBody
public List<Testuser> query(){
List<Testuser> userlist = servicetest.getAllUser();
return userlist;
}
/*
* 添加用户信息
*
* */
@RequestMapping("/add")
@ResponseBody
public boolean add(){
return servicetest.addoneuser();
}
}
Service
package com.xie.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xie.dao.Mapperuser;
import com.xie.entity.Testuser;
@Service
public class ServiceTest {
@Autowired
private Mapperuser mapperuser;
/*查询所有用户信息*/
public List<Testuser> getAllUser(){
List<Testuser> list = mapperuser.queryTestInfo();
return list;
}
/*插入一条数据*/
public boolean addoneuser(){
/*模拟post提交数据*/
Testuser user=new Testuser();
user.setId(4);
user.setUsername("新增数据");
user.setPassword("www.123");
user.setAddress("北京");
return mapperuser.adduser(user);
}
}
Dao
package com.xie.dao;
import java.util.List;
import com.xie.entity.Testuser;
import tk.mybatis.mapper.common.Mapper;
public interface Mapperuser extends Mapper<Testuser> {
/*查询用户信息*/
List<Testuser> queryTestInfo();
/*新增用户信息*/
boolean adduser(Testuser user);
}
entity实体类
package com.xie.entity;
public class Testuser {
private Integer id;
private String username;
private String password;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Testuser [id=" + id + ", username=" + username + ", password=" + password + ", address=" + address
+ "]";
}
}