1.创建项目模块

2. 选择创建的项目类型,maven的web项目

3.输入项目名

4.选择自己配置的本地maven库

5.创建项目

6.查看项目结构

7.配置项目的依赖pom.xml

 <?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">
<parent>
<artifactId>SpringOne_MyBatis</artifactId>
<groupId>com.kgc</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../SpringOne_MyBatis/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>SpringAnnotation_Servlet_MyBatis</artifactId>
<packaging>war</packaging> <name>SpringAnnotation_Servlet_MyBatis Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
<!--2.jsp和servlet资源包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--mybatis整合spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.1</version>
</dependency>
<!--spring-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.jdbc</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.orm</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.transaction</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
</dependencies>
</project>

8.项目结构如下

9.创建Classes.java

 package classes.pojo;
import org.springframework.stereotype.Repository;
import student.pojo.Student; import java.util.List;
/**
* 一方:班级表
* 一对多:需要在一方引入多方list集合
* 在一方存在一对多的关系
* 比如一个班级下多个学生
*/
public class Classes {
private Integer cid;
private String cname;
//一对多配置
private List<Student> stulist; public Classes() {
} public Classes(Integer cid, String cname) {
this.cid = cid;
this.cname = cname;
} public Classes(Integer cid, String cname, List<Student> stulist) {
this.cid = cid;
this.cname = cname;
this.stulist = stulist;
} public Integer getCid() {
return cid;
} public void setCid(Integer cid) {
this.cid = cid;
} public String getCname() {
return cname;
} public void setCname(String cname) {
this.cname = cname;
} @Override
public String toString() {
return "Classes{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", stulist=" + stulist +
'}';
}
}

10.创建ClassesMapper.java

 package classes.mapper;

 import classes.pojo.Classes;

 import java.util.List;

 /**
* 一方:数据库访问层的映射接口==dao接口
*/
public interface ClassesMapper {
//1.查询所有
List<Classes> selectAll();
//2.根据主键查询对象
Classes selectByPrimaryKey(Integer cid);
//3.添加对象
int insertData(Classes classes);
//4.修改对象
int updateData(Classes classes);
//5.删除对象
int deleteByPrimaryKey(Integer cid);
}

10.在resources下创建ClassesMapper.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">
<!--
(重点)
映射xml文件的命名空间:在整个项目总,所有的映射xml文件的namespace值必须是唯一的
如果是无代理,则namespace的值是任意的,但是一般情况下都写映射接口的全路径
如果是有代理,则namespace的值必须是映射接口的全路径
-->
<mapper namespace="classes.mapper.ClassesMapper">
<!--1.查询所有-->
<!--
id:值和映射接口中方法名一一对应
resultType:值和映射接口中方法返回值类型一一对应
parameterType:值和映射接口中方法参数类型一一对应
-->
<select id="selectAll" resultMap="classesMap">
SELECT * FROM STUDENT S,CLASSES C WHERE S.CID=C.CID
</select>
<!--id值必须和某个select节点的id值保持一致,-->
<!--type的值取决于当前是以哪一方为主,如果一方为主,则配置一方类类型,如果多方为主,则配置多方类类型-->
<!--resultmap中的配置和持久化类属性的配置一一对应,换言之,持久化类有多少属性,则resultMap将配置多少个节点-->
<resultMap id="classesMap" type="Classes">
<!--配置一方主键列和一方持久化对象的属性映射-->
<!--column:表示select节点中select查询语句中显示的列名或别名,-->
<!--如果无别名,column表示的是列名,如果有别名,则column表示的是别名;-->
<!--property:表示数据库某一列的值和持久化对象中哪个属性对应,则填写哪个属性-->
<!--id节点表示主键映射配置-->
<id column="CID" property="cid"/>
<!--配置一方非主键列和一方持久化对象的其他属性映射-->
<!--result节点表示的是非主键映射配置-->
<result column="CNAME" property="cname"/>
<!--一对多配置,在持久化类一方引入多方list集合的地方,映射xml文件中将配置collection节点-->
<!--ofType表示多方集合中的类类型-->
<collection property="stulist" ofType="Student" resultMap="studentMap"/>
</resultMap>
<!--student集合配置-->
<resultMap id="studentMap" type="Student">
<!--配置多方主键映射-->
<id column="SID" property="sid"/>
<!--配置多方非主键映射-->
<!--column属性值必须不能有空格,否则启动扫描就报错,没有get/set-->
<result column="SNAME" property="sname"/>
<result column="PASSWORD" property="password"/>
<result column="SUBJECT" property="subject"/>
<result column="GOAL" property="goal"/>
</resultMap>
<!--2.根据主键查询对象-->
<select id="selectByPrimaryKey" parameterType="Integer" resultType="Classes">
select * from classes where cid=#{cid}
</select>
<!--3.添加对象-->
<insert id="insertData" parameterType="classes.pojo.Classes">
insert into classes(cname) VALUES (#{cname})
</insert>
<!--4.修改对象-->
<update id="updateData" parameterType="classes.pojo.Classes">
update classes set cname=#{cname} where cid=#{cid}
</update>
<!--5.删除对象-->
<delete id="deleteByPrimaryKey" parameterType="Integer">
delete from classes where cid=#{cid}
</delete>
</mapper>

11.创建ClassesMapperImpl.java实现类

 package classes.mapper;
import classes.pojo.Classes;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import java.util.List;
/**
* Created by Administrator on 2018/10/10.
*/
/*Repository表示会在spring容器中产注册一个
<bean id="classesMapperImpl" class="classes.mapper.ClassesMapperImpl"/>
*/
@Repository
public class ClassesMapperImpl implements ClassesMapper{
// 向spring容器要class属性为SqlSessionTemplate的bean节点对象
@Autowired
private SqlSessionTemplate sqlSession; @Override
public List<Classes> selectAll() { //2.获取映射接口的代理对象
ClassesMapper classesMapperProxy=sqlSession.getMapper(ClassesMapper.class);
//3.找映射接口的代理对象去获取数据
List<Classes> list=classesMapperProxy.selectAll();
//5.返回数据
return list;
} @Override
public Classes selectByPrimaryKey(Integer cid) { //2.获取映射接口的代理对象
ClassesMapper classesMapperProxy=sqlSession.getMapper(ClassesMapper.class);
//3.找映射接口的代理对象去获取数据
Classes classes=classesMapperProxy.selectByPrimaryKey(cid);
return classes;
} @Override
public int insertData(Classes classes) { //2.获取映射接口的代理对象
ClassesMapper classesMapperProxy=sqlSession.getMapper(ClassesMapper.class);
//3.找映射接口的代理对象去获取数据
int num=classesMapperProxy.insertData(classes);
return num;
} @Override
public int updateData(Classes classes) { //2.获取映射接口的代理对象
ClassesMapper classesMapperProxy=sqlSession.getMapper(ClassesMapper.class);
//3.找映射接口的代理对象去获取数据
int num=classesMapperProxy.updateData(classes);
return num;
} @Override
public int deleteByPrimaryKey(Integer cid) { //2.获取映射接口的代理对象
ClassesMapper classesMapperProxy=sqlSession.getMapper(ClassesMapper.class);
//3.找映射接口的代理对象去获取数据
int num=classesMapperProxy.deleteByPrimaryKey(cid);
return num;
}
}

12.创建ClassesService.java接口

 package classes.service;

 import classes.pojo.Classes;

 import java.util.List;

 /**
* 一方:业务层的接口
*/
public interface ClassesService {
//1.查询所有
List<Classes> showData();
//2.根据主键查询对象
Classes showInfo(Integer cid);
//3.添加对象
int addData(Classes classes);
//4.修改对象
int editData(Classes classes);
//5.删除对象
int deleteData(Integer cid);
}

13.创建ClassesServiceImpl.java实现类

 package classes.service;

 import classes.mapper.ClassesMapper;
import classes.pojo.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; /**
* Created by Administrator on 2018/10/17.
*/
/*Service表示会在spring容器中注册一个
<bean id="classesServiceImpl" class="classes.service.ClassesServiceImpl"/>
*/
@Service
public class ClassesServiceImpl implements ClassesService{
// 向spring容器要class属性为ClassesMapper类型或其子类类型的bean节点对象
@Autowired
private ClassesMapper mapper; @Override
public List<Classes> showData() {
return mapper.selectAll();
} @Override
public Classes showInfo(Integer cid) {
return mapper.selectByPrimaryKey(cid);
} @Override
public int addData(Classes classes) {
return mapper.insertData(classes);
} @Override
public int editData(Classes classes) {
return mapper.updateData(classes);
} @Override
public int deleteData(Integer cid) {
return mapper.deleteByPrimaryKey(cid);
}
}

14.创建ClassesServlet.java

 package classes.controller;

 import classes.pojo.Classes;
import classes.service.ClassesService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List; /**
* Created by Administrator on 2018/10/17.
*/
public class ClassesServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.乱码处理
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//2.接受请求参数
//3.请求处理
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//调用注解在spring容器中注册的Service层的bean的id
ClassesService classesService=(ClassesService)context.getBean("classesServiceImpl");
List<Classes> claList=classesService.showData();
//4.请求跳转
if(claList!=null){
System.out.println("classes find all Success");
request.setAttribute("claList",claList);
request.getRequestDispatcher("index.jsp").forward(request,response);
}else{
System.out.println("classes find all error"); } }
}

15.配置web.xml文件

 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<servlet>
<servlet-name>ClassesServlet</servlet-name>
<servlet-class>classes.controller.ClassesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ClassesServlet</servlet-name>
<url-pattern>/ClassesServlet</url-pattern>
</servlet-mapping> </web-app>

16. 在resoureces下创建applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.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
">
<!--全局扫描包:设置底层扫描的活动范围-->
<context:component-scan base-package="classes.*,student.*"/>
<!--全局注解参数:在扫描的活动范围内找有那些类中有@符号,并在去创建bean节点,准备项目运行期间的资源对象和准备工作-->
<context:annotation-config/>
<!--驱动管理数据源-->
<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/kgc"/>
<property name="username" value="root"/>
<property name="password" value="ok"/>
</bean>
<!--sqlsessionfacotybean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--引入映射文件-->
<property name="mapperLocations">
<list>
<value>ClassesMapper.xml</value>
<value>StudentMapper.xml</value>
</list>
</property>
<property name="typeAliases">
<list>
<value>classes.pojo.Classes</value>
<value>student.pojo.Student</value>
</list>
</property>
</bean>
<!--SqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!--SqlSessionTemplate sqlSessionTemplate=new sqlSessionTemplate(sqlSessionFactory);-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean> </beans>

17. 编写index.jsp页面

 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>班级信息列表</title>
</head>
<body>
<table>
<tr>
<td>班级编号</td>
<td>班级名称</td>
<td>操作</td>
</tr>
<c:forEach var="i" items="${claList}">
<tr>
<td>${i.cid}</td>
<td>${i.cname}</td>
<td>
<a href="#">修改</a>
|
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>

18.部署工程

3.IDEA+Maven+Spring注解+Servlet+MyBatis的更多相关文章

  1. spring boot集成mybatis框架

    概述 中文官网:http://www.mybatis.cn 参考教程:https://www.w3cschool.cn/mybatis MyBatis Plus:http://mp.baomidou. ...

  2. Maven+Spring MVC Spring Mybatis配置

    环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...

  3. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  4. Eclipse完成Maven + Spring Boot + Mybatis + jsp

    Spring Boot 完成WEB项目开发 开发工具:eclipse 框架:Maven:Spring Boot:Mybatis 界面:jsp:javascript:css 前言: 在SpringBoo ...

  5. eclipse-搭建maven的war项目集合spring注解方式

    工具:eclipse 4.4.2 版本号:20150219-0600 jdk:1.7 1.下图创建maven工程,然后next 下图选择工程保存位置(这里选择默认),next 下图选择webapp项目 ...

  6. 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目

    研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...

  7. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  8. Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用

    Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...

  9. mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下)

    继续 mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上) 五.使用监听器启动Spring容器 1.修改pom.xml文件,添加Spring-we ...

随机推荐

  1. selenium获取弹窗提示

    1.点击保存给的提示是几秒钟,遮罩显示 2. 其他弹窗处理方法 http://blog.csdn.net/Real_Tino/article/details/59068827

  2. 深入分析JavaWeb Item47 -- Struts2拦截器与文件上传下载

    一.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 过滤器VS拦截器功能是一回事. 过滤器是Servlet规范中的技术,能够对请求和响应进行过滤. 拦截器是Struts2框架中的技术. ...

  3. java 将byte[]转为各种进制的字符串

    public void test() { byte[] bytes = new byte[10000000]; for (int i = 0; i < 10000000; i++) { if ( ...

  4. Android shape自定义形状,设置渐变色

      <?xml version="1.0" encoding="utf-8"?> <!-- android:shape=["rect ...

  5. How to do IF NOT EXISTS in SQLite

    http://stackoverflow.com/questions/531035/how-to-do-if-not-exists-in-sqlite How about this? INSERT O ...

  6. Codeforces Round #402 D(二分)

                                                                                         D. String Game ...

  7. P2532 [AHOI2012]树屋阶梯 卡特兰数

    这个题是一个卡特兰数的裸题,为什么呢?因为可以通过划分来导出递推式从而判断是卡特兰数,然后直接上公式就行了.卡特兰数的公式见链接. https://www.luogu.org/problemnew/s ...

  8. php write excel

    /** * 写excel方法 */ function writeExcel($tabArr, $dataArr,$path) { require_once CODE_BASE2 . '/util/ph ...

  9. form内部的button_to不submit

    创建: 2017/09/12 更新: 2018/03/17 修正因为博客迁移造成的格式问题 官方文档 http://railsdoc.com/references/button_to 参考文档 htt ...

  10. C/C++中输入多组数据方法

    --------开始-------- 对于刚开始学编程的人来说每次基本上就是一次数据输入,多次的话基本也是会给定一个数组的大小,但随着做刷算法题开始,题目有的会不直接告诉输入几组数据,基本输入都是多组 ...