使用sts搭建ssm项目

1.右键 new -> SpringProject

templates选择  Spring MVC  Project

创建完成如下图所示

2.修改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 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.my</groupId>
	<artifactId>rental</artifactId>
	<name>CarRentalStore</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.8</java-version>
		<org.springframework-version>4.3.5.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
		<!-- mysql驱动版本号 -->
		<mysql-driver.version>5.1.47</mysql-driver.version>
	</properties>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.5</version>
		</dependency>
		<!-- 添加mysql驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-driver.version}</version>
		</dependency>
		<!-- 添加数据库连接池依赖 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- 添加通用commons -->
		<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-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.9</version>
		</dependency>
		<!-- 添加MyBatis -->
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework.batch/spring-batch-infrastructure -->
		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-infrastructure</artifactId>
			<version>3.0.7.RELEASE</version>
		</dependency>
		<!-- 分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.1.6</version>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
			<version>0.9.5</version>
		</dependency>

		<!-- 添加Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- 添加fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.41</version>
		</dependency>
		<!-- 映入JSON -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.8.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.0</version>
		</dependency>

		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>

		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>

		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
			<scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
		
		
		<dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.6.Final</version> 
        </dependency>
        
	</dependencies>
	<!-- 编译 -->
	<build>
		<!-- 编译时的插件 -->
		<plugins>
			<plugin>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>2.9</version>
				<configuration>
					<additionalProjectnatures>
						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
					</additionalProjectnatures>
					<additionalBuildcommands>
						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
					</additionalBuildcommands>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<compilerArgument>-Xlint:all</compilerArgument>
					<showWarnings>true</showWarnings>
					<showDeprecation>true</showDeprecation>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.2.1</version>
				<configuration>
					<mainClass>org.test.int1.Main</mainClass>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.5</version>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${mysql-driver.version}</version>
					</dependency>
				</dependencies>
				<configuration>
					<!--配置文件的路径 -->
					<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

3.修改spring以及springMVC的配置文件

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<context:component-scan base-package="com.my.controller" />
	
	
	
</beans:beans>

root-context.xml

在这个配置文件的Namespace中勾选如下选项

<?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 https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<context:annotation-config/>
	<!-- 扫描注解包位置 -->
	<context:component-scan base-package="com.my.controller,com.my.mapper,com.my.service"/>
	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- 引入jdbc配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!--创建jdbc数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="url" value="${jdbc.url}" />
	</bean>
	<!-- 配置MyBatis的sqlSession -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<property name="dataSource" ref="dataSource" />
		<!-- 加载分页插件 -->
		<property name="plugins">
			<array>
				<bean class="com.github.pagehelper.PageHelper">
					<property name="properties">
						<value>
							dialect=mysql
							reasonable=true
						</value>
					</property>
				</bean>
			</array>
		</property>
	</bean>
	<!-- 映射Mapper目录 -->
	<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.my.mapper" />
	</bean>

	<!-- 可通过注解控制事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>	
</beans>

4.添加配置文件

mybatis-config.xml (关于mybatis,这个配置文件内容为空,因为关于mybatis的配置已经交给spring管理)

jdbc.properties (关于数据库连接)

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/cartest?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
maxActive=50
maxIdle=20
maxWait=60000

generatorConfig.xml(关于反向生成)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
	<!-- JDBC数据库驱动jar文件 -->
	<classPathEntry location="E:\MySql\mysql-connector-java-5.1.47.jar" />
	<context id="context1" defaultModelType="flat">

		<!-- PO序列化 -->
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
		</plugin>

		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>

		<!-- 配置一个连接 -->
		<jdbcConnection 
			driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/carrental" 
			userId="root"
			password="root">
		</jdbcConnection>

		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!--生成Model类存放位置 -->
		<javaModelGenerator targetPackage="com.my.domain"
			targetProject="src/main/java">
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!--生成映射文件存放位置 -->
		<sqlMapGenerator targetPackage="com.my.mapper"
			targetProject="src/main/java">
			<property name="trimStrings" value="true" />
		</sqlMapGenerator>

		<!--生成Dao类存放位置 -->
		<javaClientGenerator targetPackage="com.my.mapper"
			targetProject="src/main/java" type="XMLMAPPER" />

		<!-- 表信息 -->
		<table schema="carrental" tableName="car" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="carstore" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="carbrand" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="cartype" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="favorable" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="favtype" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="orderform" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="ordstatus" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="cususer" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="misuser" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="role" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="role_per" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>
		<table schema="carrental" tableName="permission" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false"
			enableSelectByExample="false" selectByExampleQueryId="false">
		</table>

	</context>
</generatorConfiguration>

5.反向生成

选中项目,右键 run as --> maven 

goals :mybatis-generator:generate

运行之后,生成的实体类及mapper如下图

6.修改实体类中关于外键的类型

数据库设计中,外键设置的是int类型,在java类中,外键字段要改成对象类型

 

7.修改mapper.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.my.mapper.FavorableMapper">
  <resultMap id="BaseResultMap" type="com.my.domain.Favorable">
    <id column="id" jdbcType="INTEGER" property="id" />
    
    <!-- 关于外键在下面做单独处理 -->
    <!-- <result column="favType" jdbcType="INTEGER" property="favtype" />
    <result column="favCar" jdbcType="INTEGER" property="favcar" /> -->
    
    <result column="favPrice" jdbcType="DOUBLE" property="favprice" />
  </resultMap>
  
  <!-- 相关外键 -->
  <resultMap type="com.my.domain.Favorable" id="Favorable" extends="BaseResultMap">
  	
  	<result column="id" jdbcType="INTEGER" property="favType.id"/>
	<result column="favname" jdbcType="VARCHAR" property="favType.favname"/>
	
	<result column="id" jdbcType="INTEGER" property="favcar.id"/>
	<result column="carname" jdbcType="VARCHAR" property="favcar.favname"/>
	<result column="carprice" jdbcType="DOUBLE" property="favcar.carprice"/>
	<result column="carnum" jdbcType="INTEGER" property="favcar.carnum"/>
	<result column="carimg" jdbcType="VARCHAR" property="favcar.carimg"/>
	
	<!-- 关于外键表中的外键 -->
	<result column="id" jdbcType="INTEGER" property="favcar.carbrand.id"/>
	<result column="brandname" jdbcType="VARCHAR" property="favcar.carbrand.brandname"/>
	<result column="id" jdbcType="INTEGER" property="favcar.cartype.id"/>
	<result column="typename" jdbcType="VARCHAR" property="favcar.cartype.typename"/>
	<result column="id" jdbcType="INTEGER" property="favcar.carstore.id"/>
	<result column="storename" jdbcType="VARCHAR" property="favcar.carstore.storename"/>
	<result column="storetel" jdbcType="VARCHAR" property="favcar.carstore.storetel"/>
	<result column="storelocation" jdbcType="VARCHAR" property="favcar.carstore.storelocation"/>
	<result column="storebushour" jdbcType="VARCHAR" property="favcar.carstore.storebushour"/>
	
  </resultMap>
  
  <sql id="Base_Column_List">
    id, favType, favCar, favPrice
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from favorable
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from favorable
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.my.domain.Favorable">
    insert into favorable (id, favType, favCar, 
      favPrice)
    values (#{id,jdbcType=INTEGER}, #{favtype.id,jdbcType=INTEGER}, #{favcar.id,jdbcType=INTEGER}, 
      #{favprice,jdbcType=DOUBLE})
  </insert>
  <insert id="insertSelective" parameterType="com.my.domain.Favorable">
    insert into favorable
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="favtype != null">
        favType,
      </if>
      <if test="favcar != null">
        favCar,
      </if>
      <if test="favprice != null">
        favPrice,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="favtype != null">
        #{favtype.id,jdbcType=INTEGER},
      </if>
      <if test="favcar != null">
        #{favcar.id,jdbcType=INTEGER},
      </if>
      <if test="favprice != null">
        #{favprice.id,jdbcType=DOUBLE},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.my.domain.Favorable">
    update favorable
    <set>
      <if test="favtype != null">
        favType = #{favtype.id,jdbcType=INTEGER},
      </if>
      <if test="favcar != null">
        favCar = #{favcar.id,jdbcType=INTEGER},
      </if>
      <if test="favprice != null">
        favPrice = #{favprice,jdbcType=DOUBLE},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.my.domain.Favorable">
    update favorable
    set favType = #{favtype.id,jdbcType=INTEGER},
      favCar = #{favcar.id,jdbcType=INTEGER},
      favPrice = #{favprice,jdbcType=DOUBLE}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

8.在mapper.java中添加自定义方法以及注释,在mapper.xml中添加对应sql

package com.my.mapper;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.my.domain.Car;

//@Repository注解的作用:被作为持久层操作(数据库)的bean来使用

@Repository("CarMapper")
public interface CarMapper {
	
    //自定义方法,在mapper.xml中要有相对应的sql
	List<Car> findAll();
	
    int deleteByPrimaryKey(Integer id);

    int insert(Car record);

    int insertSelective(Car record);

    Car selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Car record);

    int updateByPrimaryKey(Car record);
}
  <select id="findAll" resultMap="Car">
  	SELECT a.*,b.typeName,c.storeTel,c.storeName,c.storeLocation,c.storeBusHour 
  	FROM car a LEFT JOIN carType b on a.carType = b.id 
	LEFT JOIN carStore c on a.carStore = c.id
  </select>

 

9.编写service

package com.my.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.my.domain.Car;
import com.my.mapper.CarMapper;

//@Service的作用:业务逻辑层注解,这个注解只是标注该类处于业务逻辑层
@Service("CarService")
public class CarService {

	/*
	 * @Autowired顾名思义,就是自动装配,其作用是为了消除代码Java
	 *           代码里面的getter/setter与bean属性中的property。
	 *           当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。
	 *           
	 *  简单来说:@Autowired 自动装配
	 *           @Qualifier("") 按名称装配         
	 */
	@Autowired@Qualifier("CarMapper")
	private CarMapper carMapper;
	
    //@Transactional表示事物
	@Transactional(readOnly=true,propagation=Propagation.REQUIRED)
	public List<Car> findAll(){
		return this.carMapper.findAll();
	}

	public CarMapper getCarMapper() {
		return carMapper;
	}

	public void setCarMapper(CarMapper carMapper) {
		this.carMapper = carMapper;
	}
	
}

 

9.编写 controller

package com.my.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.my.domain.Car;
import com.my.service.CarService;

/*
 * @Controller : spring-mvc的注解,具有将请求进行转发,重定向的功能。
 * @RequestMapping: 一个用来处理请求地址映射的注解,可用于类或者方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
 */
@Controller("CarController")
@RequestMapping("/car")
public class CarController {

	@Autowired@Qualifier("CarService")
	private CarService carService;
	@RequestMapping("findAll")
	public String findAll(Model model){
		List<Car> carList = this.carService.findAll();
		model.addAttribute("carList", carList);
		
		//表示跳转到views文件夹下的xx路径
		return "car/show";
	}
	
	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}
	
}

 

 

10.编写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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach items="${ requestScope.carList}" var="car">
	${car.carname }------${car.carprice } <br>
	${car.carnum }------${car.carimg } <br>
	${car.carbrand.brandname }------${car.cartype.typename } <br>
	${car.carstore.storename }------${car.carstore.storetel } <br>
	${car.carstore.storelocation }------${car.carstore.storebushour } <br>
</c:forEach>

</body>
</html>

 

11.启动服务,在浏览器输入:http://localhost:8080/rental/car/findAll

完成一个简易的ssm小项目

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值