mysql外键的mapper配置,使用sts搭建ssm项目

1.右键 new -> SpringProject

5edb613893cc8dc50a728133b4491cc7.png

templates选择  Spring MVC  Project

创建完成如下图所示

3bedf175a0c3f5d76bb2fb20785bb9ed.png

2.修改pom.xml文件

4.0.0

com.my

rental

CarRentalStore

war

1.0.0-BUILD-SNAPSHOT

1.8

4.3.5.RELEASE

1.6.10

1.6.6

5.1.47

org.mybatis.generator

mybatis-generator-core

1.3.5

mysql

mysql-connector-java

${mysql-driver.version}

commons-dbcp

commons-dbcp

1.2.2

commons-fileupload

commons-fileupload

1.3.1

commons-io

commons-io

2.4

commons-codec

commons-codec

1.9

org.mybatis

mybatis

3.4.5

org.mybatis

mybatis-spring

1.3.0

org.springframework.batch

spring-batch-infrastructure

3.0.7.RELEASE

com.github.pagehelper

pagehelper

4.1.6

com.github.jsqlparser

jsqlparser

0.9.5

org.springframework

spring-core

${org.springframework-version}

org.springframework

spring-web

${org.springframework-version}

org.springframework

spring-oxm

${org.springframework-version}

org.springframework

spring-tx

${org.springframework-version}

org.springframework

spring-jdbc

${org.springframework-version}

org.springframework

spring-webmvc

${org.springframework-version}

org.springframework

spring-context

${org.springframework-version}

org.springframework

spring-context-support

${org.springframework-version}

org.springframework

spring-aop

${org.springframework-version}

org.springframework

spring-test

${org.springframework-version}

com.alibaba

fastjson

1.1.41

org.codehaus.jackson

jackson-mapper-asl

1.9.13

com.fasterxml.jackson.core

jackson-core

2.8.0

com.fasterxml.jackson.core

jackson-databind

2.8.0

org.aspectj

aspectjrt

${org.aspectj-version}

org.slf4j

slf4j-api

${org.slf4j-version}

org.slf4j

jcl-over-slf4j

${org.slf4j-version}

runtime

org.slf4j

slf4j-log4j12

${org.slf4j-version}

runtime

log4j

log4j

1.2.15

javax.mail

mail

javax.jms

jms

com.sun.jdmk

jmxtools

com.sun.jmx

jmxri

runtime

javax.inject

javax.inject

1

javax.servlet

servlet-api

3.0-alpha-1

provided

javax.servlet.jsp

jsp-api

2.2

provided

javax.servlet

jstl

1.2

junit

junit

4.7

test

io.netty

netty-all

4.1.6.Final

maven-eclipse-plugin

2.9

org.springframework.ide.eclipse.core.springnature

org.springframework.ide.eclipse.core.springbuilder

true

true

org.apache.maven.plugins

maven-compiler-plugin

2.5.1

1.8

1.8

-Xlint:all

true

true

org.codehaus.mojo

exec-maven-plugin

1.2.1

org.test.int1.Main

org.mybatis.generator

mybatis-generator-maven-plugin

1.3.5

mysql

mysql-connector-java

${mysql-driver.version}

${basedir}/src/main/resources/generatorConfig.xml

true

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

90210c5ac17ad4f437ac3d1a98532deb.png

servlet-context.xml

root-context.xml

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

9cd02bb1e0a1436b1b8a1979b256b23a.png

dialect=mysql

reasonable=true

4.添加配置文件

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

5d84a21a5682822e841aa5cc61dd725b.png

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(关于反向生成)

5.反向生成

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

goals :mybatis-generator:generate

b090bb0a8b6bd2dd3dacb458bd5e494d.png

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

0b021c59f8622ce161d1be70282a827d.png

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

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

377e7645e3c24cf468c977579fa23505.png

7.修改mapper.xml中的相关外键配置

举出其中一个例子,注意相关外键的转换,以及外键表中的外键

id, favType, favCar, favPrice

select

from favorable

where id = #{id,jdbcType=INTEGER}

delete from favorable

where id = #{id,jdbcType=INTEGER}

insert into favorable (id, favType, favCar,

favPrice)

values (#{id,jdbcType=INTEGER}, #{favtype.id,jdbcType=INTEGER}, #{favcar.id,jdbcType=INTEGER},

#{favprice,jdbcType=DOUBLE})

insert into favorable

id,

favType,

favCar,

favPrice,

#{id,jdbcType=INTEGER},

#{favtype.id,jdbcType=INTEGER},

#{favcar.id,jdbcType=INTEGER},

#{favprice.id,jdbcType=DOUBLE},

update favorable

favType = #{favtype.id,jdbcType=INTEGER},

favCar = #{favcar.id,jdbcType=INTEGER},

favPrice = #{favprice,jdbcType=DOUBLE},

where id = #{id,jdbcType=INTEGER}

update favorable

set favType = #{favtype.id,jdbcType=INTEGER},

favCar = #{favcar.id,jdbcType=INTEGER},

favPrice = #{favprice,jdbcType=DOUBLE}

where id = #{id,jdbcType=INTEGER}

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

ListfindAll();

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 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

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 ListfindAll(){

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){

ListcarList = 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;

}

}

b591669dd707552b1227ff07128545ff.png

10.编写jsp页面

Insert title here

${car.carname }------${car.carprice }

${car.carnum }------${car.carimg }

${car.carbrand.brandname }------${car.cartype.typename }

${car.carstore.storename }------${car.carstore.storetel }

${car.carstore.storelocation }------${car.carstore.storebushour }

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

完成一个简易的ssm小项目

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值