eclipse搭建ssm工程

前言:

今天回顾了一下eclipse 搭建ssm项目,感觉真实太麻烦了,配置完成之后,报各种错误,但是检查了好几遍没有错误,然后发现eclipse加载实在是太慢了,可能是我网络问题吧,习惯了IDEA还有点不习惯,后来等eclipse加载好各种配置之后,就发现没有问题了。

来总结一下配置吧↓

1.使用eclipse创建maven项目

问题!!!—> 创建webapp时报错,(这里就不截图了),就是创建不了
解决!!!—> maven仓库出现问题,把org\apache\maven\archetypes\maven-archetype-webapp删除从新下载即可!!!

2.配置pom文件(修改jdk,添加tomcat 略)
<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>com.zt</groupId>
	<artifactId>Maven_SSM</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Maven_SSM Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<!-- 定义的版本号 -->
	<properties>
		<spring-version>5.0.7.RELEASE</spring-version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>
		<!-- mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<!-- spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<!-- spring-mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-version}</version>
		</dependency>

		<!-- 文件下载 -->
		<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- JSTL -->
		<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/log4j/log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>Maven_SSM</finalName>
	</build>
</project>

3.配置xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	
	欢迎界面
	<welcome-file-list>
		<welcome-file>tea/show.do</welcome-file>
	</welcome-file-list>
	<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/*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	配置过滤器
	<filter>
		<filter-name>characterEncodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 报错异常 -->
	<error-page>
		<!-- 状态吗 -->
		<error-code>404</error-code>
		<location>/404.html</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/500.html</location>
	</error-page>
	<error-page>
		<error-code>400</error-code>
		<location>/400.html</location>
	</error-page>
</web-app>
4.配置Spring.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:util="http://www.springframework.org/schema/util" 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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util 
		http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 寻找链接数据库的配置 -->
	
	
	<util:properties id="jdbc" location="classpath:db.properties" />
	<!--加载数据源,数据源使用了db.properties文件内容-->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="#{jdbc.url}"></property>
		<property name="driverClassName" value="#{jdbc.driverClassName}"></property>
		<property name="username" value="#{jdbc.username}"></property>
		<property name="password" value="#{jdbc.password}"></property>
	</bean>
	<!-- 创建数据库工厂,数据库工厂中需要数据源,映射文件,配置文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
		<property name="configLocation" value="classpath:mybatis.xml"></property>
	</bean>
	<!-- 将mapper包和数据库工厂结合在一起 -->
	<bean id="" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.zhiyou100.mapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
	<!-- 创建事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启事务 -->
	<tx:annotation-driven/>

</beans>
5.配置SpringMVC.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
						http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
	<!-- 开启注解 -->
	<mvc:annotation-driven />
	<!-- 开启扫描包 -->
	<context:component-scan base-package="com.zhiyou100"/>
	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	<!-- 上传文件 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="104857600" />
		<property name="maxInMemorySize" value="4096" />
		<property name="defaultEncoding" value="UTF-8"></property>
	</bean>
	
	

</beans>
6.配置Mybatis.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>


		<!-- 开启mybatis二级缓存 -->
		<setting name="cacheEnabled" value="true" />
		<!-- 开启log4j -->
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>
	别名
	<typeAliases>
		<typeAlias type="com.zhiyou100.model.Tea" alias="Tea" />
		<typeAlias type="com.zhiyou100.model.Stu" alias="Stu" />
	</typeAliases>


</configuration>
db.properties
url=jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8
driverClassName=com.mysql.jdbc.Driver
username=root
password=123456
log4j.properties
log4j.rootLogger = debug,stdout,D,E


log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n


log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n


log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n




log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
pojo
package com.zhiyou100.model;

import java.io.Serializable;

public class Stu implements Serializable{
	/**
	 * 二级缓存必须加id
	 */
	private static final long serialVersionUID = 744337370857966252L;
	private Integer id;
	private String name;
	private Integer teaId;
	private Tea tea;
	@Override
	public String toString() {
		return "Stu [id=" + id + ", name=" + name + ", teaId=" + teaId + ", tea=" + tea + "]";
	}
	public Stu(Integer id, String name, Integer teaId, Tea tea) {
		super();
		this.id = id;
		this.name = name;
		this.teaId = teaId;
		this.tea = tea;
	}
	public Stu() {
		super();
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getTeaId() {
		return teaId;
	}
	public void setTeaId(Integer teaId) {
		this.teaId = teaId;
	}
	public Tea getTea() {
		return tea;
	}
	public void setTea(Tea tea) {
		this.tea = tea;
	}

	

}

package com.zhiyou100.model;

import java.io.Serializable;
import java.sql.Date;
import java.util.List;

public class Tea implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8339364943482614416L;
	private Integer id;
	private String name;
	private Date creatTime;
	private String sex;
	private List<Stu> stus;
	private Integer index;

	public Tea(Integer id, String name, Date creatTime, String sex, List<Stu> stus, Integer index) {
		super();
		this.id = id;
		this.name = name;
		this.creatTime = creatTime;
		this.sex = sex;
		this.stus = stus;
		this.index = index;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreatTime() {
		return creatTime;
	}

	public void setCreatTime(Date creatTime) {
		this.creatTime = creatTime;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public List<Stu> getStus() {
		return stus;
	}

	public void setStus(List<Stu> stus) {
		this.stus = stus;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Tea() {
		super();
	}

	@Override
	public String toString() {
		return "Tea [id=" + id + ", name=" + name + ", creatTime=" + creatTime + ", sex=" + sex + ", stus=" + stus
				+ ", index=" + index + "]";
	}

}

mapper

BaseMapper.java

package com.zhiyou100.mapper;

import java.util.List;

public interface BaseMapper<E> {
	
	List<E> queryAll();
	int add(E e);
	int deleteById(int id);
	E findById(int id);
	int update(E e);
	
}


StuMapper.java

package com.zhiyou100.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.zhiyou100.model.Stu;
@Mapper
public interface StuMapper extends BaseMapper<Stu>{

}

TeaMapper.java

package com.zhiyou100.mapper;

import java.sql.Date;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.zhiyou100.model.Tea;

@Mapper
public interface TeaMapper extends BaseMapper<Tea> {
	public int findMaxIndex();
	//查询我上一条的内容
	public Tea findUpIndex(Integer id);
	//查询我下一条的内容
	public Tea findDownIndex(Integer id);
	public List<Tea> findByName(String name);
	public List<Tea> findByTea(Tea tea);
	public List<Tea> findByTime(@Param("begin") Date begin,@Param("end") Date end);
	public List<Tea> findByAll(@Param("begin") Date begin,@Param("end") Date end,@Param("name") String name,@Param("sex") String sex);
}

mapper.xml

stu.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.mapper.StuMapper">
	<cache /><!-- 开启缓存 -->
	<resultMap type="com.zhiyou100.model.Stu" id="stu">
		<id column="id" property="id" />
		<result column="name" property="name" />
		<result column="tea_id" property="teaId" />
		<!-- ofType一对多 Javatype 一对一 -->
		<collection property="tea" ofType="com.zhiyou100.model.Tea">
			<id column="tid" property="id" />
			<result column="tname" property="name" />
			<result column="tcreat_time" property="creatTime" />
		</collection>
	</resultMap>
	<select id="queryAll" resultMap="stu">
		SELECT stu.*,tea.id tid,
		tea.name tname, tea.creat_time tcreat_time FROM stu
		INNER JOIN tea ON
		stu.tea_id = tea.id

	</select>
	<insert id="add" parameterType="com.zhiyou100.model.Stu">
		insert into stu
		values(null,#{name},#{teaId});
	</insert>
	<delete id="deleteById" parameterType="int">
		DELETE FROM stu WHERE id = #{id}
	</delete>
	<select id="findById" resultMap="stu" parameterType="int">
		select * from stu where id = #{id}
	</select>
	<update id="update" parameterType="com.zhiyou100.model.Stu">
		UPDATE stu SET `name`=#{name}, tea_id =#{teaId} where id = #{id}
	</update>
</mapper>

tea.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.mapper.TeaMapper">
	<cache /><!-- 开启二级缓存 -->
	<!-- 结果集映射 只能用于返回结果中 -->
	<resultMap type="com.zhiyou100.model.Tea" id="tea">
		<!-- 数据库名 Java中名 -->
		<id column="id" property="id" />
		<result column="name" property="name" />
		<result column="creat_time" property="creatTime" />
		<result column="sex" property="sex" />
		<result column="index" property="index" />
		<collection property="stus" ofType="com.zhiyou100.model.Stu">
			<id column="sid" property="id" />
			<result column="sname" property="name" />
			<result column="stea_id" property="teaId" />
		</collection>
	</resultMap>
	<!-- 模糊查询名字 -->
	<select id="findByName" parameterType="string" resultMap="tea">
		SELECT
		tea.*,stu.id sid,
		stu.`name` sname, stu.tea_id stea_id FROM tea LEFT
		JOIN stu ON tea.id
		= stu.tea_id where tea.`name` LIKE
		concat('%',#{name},'%')
	</select>
	<!-- 查询名字+性别 -->
	<select id="findByTea" parameterType="com.zhiyou100.model.Tea"
		resultMap="tea">
		SELECT
		tea.*,stu.id sid,
		stu.`name` sname, stu.tea_id stea_id
		FROM tea LEFT
		JOIN stu ON tea.id
		= stu.tea_id where tea.`name` LIKE
		concat('%',#{name},'%') and tea.sex=#{sex}
	</select>
	<!-- 根据时间查询 -->
	<select id="findByTime" parameterType="date" resultMap="tea">
		SELECT tea.*,stu.id sid,
		stu.`name` sname, stu.tea_id stea_id FROM tea
		LEFT JOIN stu ON tea.id
		= stu.tea_id
		<where>
			<if test="begin != null">
				tea.creat_time &gt;= #{begin}
			</if>
			<if test="end != null">
				and tea.creat_time &lt;= #{end}
			</if>

		</where>
	</select>
	<select id="findByAll" resultMap="tea">
		SELECT tea.*,stu.id sid,
		stu.`name` sname, stu.tea_id stea_id FROM tea
		LEFT JOIN stu ON tea.id
		= stu.tea_id
		<where>
			<if test="begin != null">
				tea.creat_time &gt;= #{begin}
			</if>
			<if test="end != null">
				and tea.creat_time &lt;= #{end}
			</if>
			<if test="name!=null">
				and tea.`name` LIKE
				concat('%',#{name},'%')
			</if>
			<if test="sex!=null">
				and tea.sex = #{sex}
			</if>

		</where>

	</select>

	<select id="queryAll" resultMap="tea">
		SELECT tea.*,stu.id sid,
		stu.`name` sname, stu.tea_id stea_id FROM tea LEFT JOIN stu ON tea.id
		= stu.tea_id ORDER BY `index`
	</select>
	<delete id="deleteById" parameterType="int">
		delete from tea where id =
		#{id}
	</delete>
	<insert id="add" parameterType="com.zhiyou100.model.Tea">
		<selectKey order="AFTER" keyProperty="id" resultType="int">
			SELECT
			LAST_INSERT_ID()
		</selectKey>
		insert into tea
		values(null,#{name},now(),#{sex},#{index});
	</insert>
	<select id="findById" parameterType="int" resultMap="tea">
		select *
		from tea where id =#{id}
	</select>
	<update id="update" parameterType="com.zhiyou100.model.Tea">
		update tea set name = #{name}
		,creat_time=#{creatTime},
		`index` = #{index}
		where id = #{id}
	</update>
	<select id="findMaxIndex" resultType="int">
		SELECT MAX(`index`) FROM
		tea
	</select>
	<!-- 查询我得上一条记录 -->
	<select id="findUpIndex" parameterType="int" resultMap="tea">
		SELECT *
		FROM tea WHERE `index` =(
		SELECT MAX(`index`) FROM tea WHERE `index`
		&lt;(
		SELECT `index` FROM tea WHERE id = #{id}))
	</select>
	<select id="findDownIndex" parameterType="int" resultMap="tea">
		SELECT * FROM tea WHERE `index` =(
		SELECT MIN(`index`) FROM tea WHERE `index` &gt;(
		SELECT `index` FROM tea WHERE id = #{index}))
	</select>
</mapper>
service

StuService.java

package com.zhiyou100.service;

import java.util.List;

import com.zhiyou100.model.Stu;

public interface StuService {
	public List<Stu> queryAll();

	public void add(Stu stu);

	public void update(Stu stu);

	public Stu findById(Integer id);

	public void deleteById(Integer id);
}

TeaService.java

package com.zhiyou100.service;

import java.util.List;

import com.zhiyou100.model.Tea;

public interface TeaService {
	public List<Tea> queryAll();

	public int add(Tea tea);

	public int update(Tea tea);

	public Tea findById(Integer id);

	public int deleteById(Integer id);

	// 根据名字搜索
	public List<Tea> findByName(String name);

	// 根据名字跟性别搜索
	public List<Tea> findByTea(Tea tea);

	// 根据日期搜索
	public List<Tea> findByTime(String begin, String end);

	// 根据所有条件搜索
	public List<Tea> findByAll(Tea tea, String begin, String end);
	//查询我上一条的内容
	public void findUpIndex(Integer id);
	//查询我下一条的内容
		public void findDownIndex(Integer id);

}

StuServiceImpl.java

package com.zhiyou100.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhiyou100.mapper.StuMapper;
import com.zhiyou100.model.Stu;
import com.zhiyou100.service.StuService;

@Service("stuService")
public class StuServiceImpl implements StuService {
	@Resource
	StuMapper stuMapper;

	public List<Stu> queryAll() {
		// TODO Auto-generated method stub
		return stuMapper.queryAll();
	}

	public void add(Stu stu) {
		// TODO Auto-generated method stub
		stuMapper.add(stu);
	}

	public void update(Stu stu) {
		// TODO Auto-generated method stub
		stuMapper.update(stu);
	}

	public Stu findById(Integer id) {
		// TODO Auto-generated method stub
		return stuMapper.findById(id);
	}

	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		stuMapper.deleteById(id);
	}

}

TeaServiceImpl.java

package com.zhiyou100.service.impl;

import java.sql.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.zhiyou100.mapper.TeaMapper;
import com.zhiyou100.model.Tea;
import com.zhiyou100.service.TeaService;

@Service("teaService")
public class TeaServiceImpl implements TeaService {
	@Resource
	TeaMapper teaMapper;

	public List<Tea> queryAll() {
		// TODO Auto-generated method stub
		return teaMapper.queryAll();
	}

	public int add(Tea tea) {
		// TODO Auto-generated method stub
		// 将最大得index数加上1 就等于 当前id得值了
		tea.setIndex(teaMapper.findMaxIndex() + 1);
		return teaMapper.add(tea);
	}

	public int update(Tea tea) {
		// TODO Auto-generated method stub
		return teaMapper.update(tea);
	}

	public Tea findById(Integer id) {
		// TODO Auto-generated method stub
		return teaMapper.findById(id);
	}

	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return teaMapper.deleteById(id);
	}

	public List<Tea> findByName(String name) {
		// TODO Auto-generated method stub
		return teaMapper.findByName(name);
	}

	public List<Tea> findByTea(Tea tea) {
		if (tea.getSex().equals("on")) {
			return teaMapper.findByName(tea.getName());
		}
		return teaMapper.findByTea(tea);
	}

	public List<Tea> findByTime(String begin, String end) {
		Date begin1 = null;
		Date end1 = null;
		try {
			begin1 = Date.valueOf(begin);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			end1 = Date.valueOf(end);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return teaMapper.findByTime(begin1, end1);
	}

	public List<Tea> findByAll(Tea tea, String begin, String end) {
		Date begin1 = null;
		Date end1 = null;
		try {
			begin1 = Date.valueOf(begin);
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			end1 = Date.valueOf(end);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (tea.getSex().equals("on")) {
			tea.setSex(null);
		}
		return teaMapper.findByAll(begin1, end1, tea.getName(), tea.getSex());
	}
	//事务开启注解
	@Transactional
	public void findUpIndex(Integer id) {
		// 拿到上一条的内容
		Tea tea = teaMapper.findUpIndex(id);
		// 当前id 内容
		Tea tea2 = teaMapper.findById(id);
		// 先拿到当前id的索引
		int index = tea2.getIndex();
		// 交换索引
		tea2.setIndex(tea.getIndex());
		// 交换
		tea.setIndex(index);
		teaMapper.update(tea2);
		//强制 代码回滚  
		//TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
		teaMapper.update(tea);
	}

	public void findDownIndex(Integer id) {
		// 拿到下一条的内容
		Tea tea = teaMapper.findDownIndex(id);
		// 当前id 内容
		Tea tea2 = teaMapper.findById(id);
		// 先拿到当前id的索引
		int index = tea2.getIndex();
		// 交换索引
		tea2.setIndex(tea.getIndex());
		// 交换
		tea.setIndex(index);
		teaMapper.update(tea2);
		teaMapper.update(tea);

	}

}

controller

AdviceController.java

package com.zhiyou100.controller;

import java.io.IOException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//@ControllerAdvice
public class AdviceController {
	//普通异常
	@ExceptionHandler(value=Exception.class)
	public String m1(){
		return "error/1";
	}
	//io 异常
	@ExceptionHandler(value=IOException.class)
	public String m2(){
		return "error/2";
	}
	//空打点异常 异常
		@ExceptionHandler(value=NullPointerException.class)
		public String m3(){
			return "error/3";
		}
}

StuController.java

package com.zhiyou100.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.model.Stu;
import com.zhiyou100.service.StuService;
import com.zhiyou100.service.TeaService;

@Controller
@RequestMapping("stu")
public class StuController {
	@Resource
	StuService stuService;
	@Resource
	TeaService teaService;

	@RequestMapping("show.do")
	public String show(Model model) {
		model.addAttribute("stus", stuService.queryAll());
		return "stu/show";
	}

	@RequestMapping("add.do")
	public String add(Model model) {
		model.addAttribute("teas", teaService.queryAll());

		return "stu/add";
	}

	@RequestMapping("insert.do")
	public String insert(Model model, Stu stu) {
		stuService.add(stu);

		return "redirect:show.do";
	}

	@RequestMapping("edit.do")
	public String edit(Integer id, Model model) {
		model.addAttribute("stu", stuService.findById(id));
		model.addAttribute("teas", teaService.queryAll());
		return "stu/update";
	}

	@RequestMapping("delete.do")
	public String delete(Integer id) {

		stuService.deleteById(id);

		return "redirect:show.do";
	}

	@RequestMapping("update.do")
	public String update(Stu stu) {
		System.out.println(stu);
		stuService.update(stu);
		return "redirect:show.do";
	}
}

TeaController.java

package com.zhiyou100.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.model.Tea;
import com.zhiyou100.service.TeaService;

@Controller
@RequestMapping("tea")
public class TeaController {
	@Resource
	TeaService teaService;

	@RequestMapping("show.do")
	public String show(Model model) {
		model.addAttribute("teas", teaService.queryAll());
		return "tea/show";
	}

	@RequestMapping("delete.do")
	public String delete(Integer id) {
		teaService.deleteById(id);
		return "redirect:show.do";
	}

	@RequestMapping("add.do")
	public String add() {

		return "tea/add";
	}

	@RequestMapping("insert.do")
	public String insert(Tea tea) {
		int ref = teaService.add(tea);
		System.out.println("打印是否添加" + ref+"++++++++"+tea);
		
		return "redirect:show.do";
	}

	@RequestMapping("edit.do")
	public String edit(Model model, Integer id) {
		model.addAttribute("tea", teaService.findById(id));
		return "tea/update";
	}

	@RequestMapping("update.do")
	public String update(Tea tea) {
		teaService.update(tea);
		return "redirect:show.do";
	}

	@RequestMapping("search.do")
	public String search(Tea tea, Model model) {
		// model.addAttribute("name", name);
		// 根据名字查询
		model.addAttribute("teas", teaService.findByTea(tea));

		return "tea/show";
	}

	// 根据日期查询
	@RequestMapping("time.do")
	public String time(String begin, String end, Model model) {
		model.addAttribute("teas", teaService.findByTime(begin, end));
		return "tea/show";
	}

	// 根据名字 性别 日期查询
	@RequestMapping("all.do")
	public String all(Tea tea, String begin, String end, Model model) {
		model.addAttribute("teas", teaService.findByAll(tea, begin, end));
		return "tea/show";
	}
	//向上移动
	@RequestMapping("up.do")
	public String up(Integer id) {
		teaService.findUpIndex(id);
		return "redirect:show.do";
	}
	
	
	//向下移动
		@RequestMapping("down.do")
		public String down(Integer id) {
			teaService.findDownIndex(id);
			return "redirect:show.do";
		}
}

view 界面

stu
add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="http://localhost:8080/Maven_SSM/">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加</title>
</head>
<body>

	<form action="stu/insert.do">
		学生:<input type="text" name="name"> 老师:<select name="teaId">
			<c:forEach var="tea" items="${teas }">
				<option value="${tea.id }">${tea.name }</option>
			</c:forEach>
		</select>
		<button>提交</button>
	</form>
</body>
</html>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="http://localhost:8080/Maven_SSM/">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<a href="stu/add.do">添加</a>
	<table class="table table-hover">
		<tr>
			<td>学生姓名</td>
			<td>老师姓名</td>
			<td>老师时间</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${stus }" var="stu">

			<tr>
				<td>${stu.name }</td>
				<td>${stu.tea.name }</td>
				<td>${stu.tea.creatTime }</td>
				<td><a href="stu/delete.do?id=${stu.id }">删除</a> <a
					href="stu/edit.do?id=${stu.id }">修改</a></td>
			</tr>
		</c:forEach>
	</table>


</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="http://localhost:8080/Maven_SSM/">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="stu/update.do">
		<input type="hidden" value="${stu.id }" name="id"> 名字<input
			type="text" name="name" value="${stu.name }"> 老师<select
			name="teaId">
			<c:forEach items="${teas }" var="tea">
				<c:if test="${stu.teaId==tea.id}">
					<option selected="selected" value="${tea.id }">${tea.name }</option>
				</c:if>
				<c:if test="${stu.teaId!=tea.id}">
					<option value="${tea.id }">${tea.name }</option>
				</c:if>
			</c:forEach>
		</select>
		<button>提交</button>
	</form>
</body>
</html>

tea
add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="http://localhost:8080/Maven_SSM/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="tea/insert.do">
		name <input name="name" type="text">
		sex  :<input name="sex" type="radio" value="男"><input name="sex" type="radio" value="女">
		<button>提交</button>
	</form>

</body>
</html>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 引入base标签 -->
<base href="http://localhost:8080/Maven_SSM/">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	所有条件搜索
	<form action="tea/all.do">
		搜索名字:<input type="text" name="name"> 搜索性别:<input type="radio"
			name="sex" value="男"><input type="radio" name="sex"
			value="女"> 不选择<input checked="checked" type="radio"
			name="sex"> 开始:<input type="date" name="begin"> 结束:<input
			type="date" name="end">
		<button>提交</button>
	</form>

	<!-- 根据名字查询 -->
	姓名搜索
	<form action="tea/search.do">
		搜索名字:<input type="text" name="name"> 搜索性别:<input type="radio"
			name="sex" value="男"><input type="radio" name="sex"
			value="女"> 不选择<input checked="checked" type="radio"
			name="sex">
		<button>搜索</button>
	</form>

	日期搜索
	<form action="tea/time.do">
		开始:<input type="date" name="begin"> 结束:<input type="date"
			name="end">
		<button>提交</button>
	</form>

	<a href="tea/add.do">添加</a>
	<table class="table table-hover">
		<tr>
			<td>序号</td>
			<td>老师</td>
			<td>时间</td>
			<td>性别</td>
			<td>学生</td>
			<td>操作</td>
		</tr>
		<!-- 设置序号 -->
		<c:set var="a" value="${0 }"></c:set>
		<c:forEach items="${teas }" var="tea">
			<c:set var="a" value="${a+1 }"></c:set>
			<tr>
				<td>${a }</td>
				<td>${tea.name }</td>
				<td>${tea.creatTime }</td>
				<td>${tea.sex }</td>
				<td>
					<!-- 如果没有学生 显示 --> <c:if
						test="${tea.stus.size()==0||tea.stus ==null }">
					没有学生
				</c:if> <!-- 只显示其中三个学生 --> <c:if
						test="${tea.stus.size()>0 && tea.stus !=null }">
						<!-- 设置数量为 0-3 -->
						<c:set var="count" value="2"></c:set>
						<!-- 如果有学生数量小于三 -->
						<c:if test="${tea.stus .size()<3 }">
							<!-- 就让循环得值 到他得数组长度 -->
							<c:set var="count" value="${tea.stus.size()-1 }"></c:set>
						</c:if>
						<!-- for循环显示 -->
						<c:forEach var="i" begin="0" end="${count }">
						${tea.stus[i].name },
					</c:forEach>
					</c:if>


				</td>
				<td>${tea.index }</td>
				<td><a href="tea/delete.do?id=${tea.id }">删除</a></td>
				<td><a href="tea/edit.do?id=${tea.id }">修改</a></td>
				<!-- 如果当前的序号不为1 就显示 -->
				<td><c:if test="${a!=1 }">
						<a href="tea/up.do?id=${tea.id }"
							class="glyphicon glyphicon-arrow-up"></a>
					</c:if> 
						<!-- 如果当前的序号 不等于所有的长度就显示 -->
					<c:if test="${a!=teas.size() }">
						<a href="tea/down.do?id=${tea.id }"
							class="glyphicon glyphicon-arrow-down"></a>
					</c:if></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="http://localhost:8080/Maven_SSM/">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="tea/update.do">
			 <input type="hidden" name="id" value="${tea.id }">
		name:<input type="text" name="name" value="${tea.name }">
		time:<input type="date" name="creatTime" value="${tea.creatTime }">
		<button>提交</button>
	</form>

</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值