Spring+SpringMVC+Mybatis整合篇
在这里推荐大家有事儿没事逛一下GitHub总会有很多信息好玩的项目被你发现.
进入正题
一、添加依赖
添加相关依赖,在这里某些依赖可能用不上(可以注释掉),但是希望有精力的话都去了解一下,许多在工作中都会用上,学无止境~
<?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.songci</groupId>
<artifactId>mytest-root</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mytest-model</module>
<module>mytest-dao</module>
<module>mytest-service</module>
<module>mytest-web</module>
</modules>
<!--添加依赖-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.6.RELEASE</spring.version>
<druid.version>1.0.5</druid.version>
<mysql.connector.version>5.1.30</mysql.connector.version>
<mybatis.version>3.2.7</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<jackson.version>1.9.13</jackson.version>
<jstl.version>1.2</jstl.version>
<commons.fileupload.version>1.3.1</commons.fileupload.version>
<commons.codec.version>1.9</commons.codec.version>
<commons.net.version>3.3</commons.net.version>
<httpclient.version>4.1.2</httpclient.version>
<aspectj.version>1.6.12</aspectj.version>
<okhttp.version>3.3.0</okhttp.version>
<commons-dbcp.version>1.4</commons-dbcp.version>
<lombok.version>1.16.18</lombok.version>
<gson.version>2.2.4</gson.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Bean注入 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 事务管理 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- ORM 框架 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 加入spring测试依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- mybatis包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- spring mybatis整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- mysql-connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- 阿里巴巴 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- jackson JSON支持 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
<!-- log4j包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- servlet api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- start apache -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons.fileupload.version}</version>
</dependency>
<!-- HTTP请求 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons.codec.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons.net.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.squareup.okhttp3</groupId>-->
<!--<artifactId>okhttp</artifactId>-->
<!--<version>${okhttp.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.projectlombok</groupId>-->
<!--<artifactId>lombok</artifactId>-->
<!--<version>${lombok.version}</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.google.code.gson</groupId>-->
<!--<artifactId>gson</artifactId>-->
<!--<version>${gson.version}</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.sitemesh</groupId>-->
<!--<artifactId>sitemesh</artifactId>-->
<!--<version>3.0.0</version>-->
<!--</dependency>-->
<!--<!–Shiro–>-->
<!--<dependency>-->
<!--<groupId>org.apache.shiro</groupId>-->
<!--<artifactId>shiro-core</artifactId>-->
<!--<version>1.3.2</version>-->
<!--</dependency>-->
<!-- dubbo -->
<!--<dependency>-->
<!--<groupId>com.alibaba</groupId>-->
<!--<artifactId>dubbo</artifactId>-->
<!--<version>2.5.3</version>-->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<artifactId>spring</artifactId>-->
<!--<groupId>org.springframework</groupId>-->
<!--</exclusion>-->
<!--</exclusions>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.apache.zookeeper</groupId>-->
<!--<artifactId>zookeeper</artifactId>-->
<!--<version>3.3.3</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.github.sgroschupf</groupId>-->
<!--<artifactId>zkclient</artifactId>-->
<!--<version>0.1</version>-->
<!--</dependency>-->
<!--<!– redis支持 –>-->
<!--<dependency>-->
<!--<groupId>org.springframework.data</groupId>-->
<!--<artifactId>spring-data-redis</artifactId>-->
<!--<version>1.0.2.RELEASE</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>redis.clients</groupId>-->
<!--<artifactId>jedis</artifactId>-->
<!--<version>2.1.0</version>-->
<!--</dependency>-->
</dependencies>
</project>
二、下载相关依赖Jar
只要点击install就会帮你下载相关依赖并放到本地仓库
三、创建相关类和映射文件
model
创建Student类
package com.songci.mytest_one.model;
/**
* Created by songl on 2017/8/8.
*/
public class Student {
private Integer id;
private String name;//姓名
private Boolean sex;//性别
private String address;//地址
public Integer getId(){return id;}
public String getName(){return name;}
public Boolean getSex(){return sex;}
public String getAddress(){return address;}
public void setId(Integer id){this.id = id;}
public void setName(String name){this.name = name;}
public void setSex(Boolean sex){this.sex = sex;}
public void setAddress(String address){this.address = address;}
@Override
public String toString() {
return "Student : id:"+this.id+" name:"+this.name+" sex:"+this.sex+" address:"+this.address;
}
}
dao
然后在mytest-dao下创建包package com.songci.mytest_one.dao
并在该包下创建StudentDao
和base这个包
,在base包下创建baseDao
定义BaseDao
package com.songci.mytest_one.dao.base;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
public interface BaseDao<T> {
public Boolean insert(T var);
public Boolean delete(T var);
public Boolean update(T var);
public List<T> select(T var);
}
StudentDao
继承BaseDao其他方法可以根据需求再添加
package com.songci.mytest_one.dao;
import com.songci.mytest_one.dao.base.BaseDao;
import com.songci.mytest_one.model.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
@Repository
public interface StudentDao extends BaseDao<Student> {
}
StudentMapper.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.songci.mytest_one.dao.StudentDao">
<insert id="insert" >
INSERT t_student(id,name,sex,address) VALUES (#{id},#{name},#{sex},#{address})
</insert>
<delete id="delete">
DELETE FROM t_student WHERE id=#{id}
</delete>
<update id="update">
UPDATE t_student
<trim prefix="SET" suffixOverrides=",">
<if test="name!= null and name !='' and name != 'null'">name=#{name},</if>
<if test="sex!= null ">sex=#{sex},</if>
<if test="address!= null and address !='' and address != 'null'">address=#{address}</if>
</trim>
WHERE id=#{id} ;
</update>
<select id="select" resultType="Student">
SELECT * FROM t_student
<trim prefix="WHERE" prefixOverrides="AND">
<if test="id!= null and id !='' ">id=#{id}</if>
<if test="name!= null and name !='' and name != 'null'">AND name=#{name}</if>
<if test="sex!= null ">AND sex=#{sex}</if>
<if test="address!= null and address !='' and address != 'null'">AND address=#{address}</if>
</trim>
</select>
</mapper>
service
创建StudentService
接口
package com.songci.mytest_one.service;
import com.songci.mytest_one.model.Student;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
public interface StudentService {
/**
* 添加学生
* @param student
* @return
*/
Boolean addStudent(Student student);
/**
* 根据ID删除学生
* @param id
* @return
*/
Boolean deleteStudentById(Integer id);
/**
* 根据ID修改学生信息
* @param student
* @return
*/
Boolean updateStudentById(Student student);
/**
* 按条件查找所有学生
* @param student
* @return
*/
List<Student> findAllStudent(Student student);
}
创建实现类StudentServiceImpl
package com.songci.mytest_one.service.impl;
import com.songci.mytest_one.dao.StudentDao;
import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
@Service("StudentService")
public class StudentServiceImpl implements StudentService{
@Resource
private StudentDao studentDao;
public Boolean addStudent(Student student) {
return studentDao.insert(student);
}
public Boolean deleteStudentById(Integer id) {
Student student=new Student();
student.setId(id);
return studentDao.delete(student);
}
public Boolean updateStudentById(Student student) {
return studentDao.update(student);
}
public List<Student> findAllStudent(Student student) {
return studentDao.select(student);
}
}
下一篇
Maven-maven多模块项目搭建+SSM框架整合(三、配置文件添加,服务层测试)
相关代码在GitHub上,包括数据库sql文件
GitHub地址:https://github.com/iamsongci/mytest_one
将持续更新 ~~~
未完待续~~~
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦