1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > idea ssm框架 mysql_IDEA 整合SSM框架(使用Maven创建工程)

idea ssm框架 mysql_IDEA 整合SSM框架(使用Maven创建工程)

时间:2023-12-06 14:00:47

相关推荐

idea ssm框架 mysql_IDEA  整合SSM框架(使用Maven创建工程)

SSM框架(Spring+SpringMVC+MyBatis)是目前Java WEB开发使用较多的框架,搭建起来起来比较麻烦,之前也曾搭建成功过。这次通过结合SSM开发的相关学习视频,再一次系统性的将整个SSM框架的搭建过程记录下来,以此来方便日后的开发。本文的前提条件,是Maven已经成功安装完毕!

搭建环境:IDEA.1

Maven3.3.9

Jdk1.7

Tomcat7.0

Mysql5.7

1、建立Maven Project

(1)新建一个Maven项目

(2)输入GroupId和ArtifactId,可随意输入,GroupId可认为是项目的包名,ArtifactId为项目的名称

(3)找到Maven安装的Home,关于Maven使用的镜像仓库是在Setting.xml中可修改的,这里注意下选择的Setting.xml的存放位置

(4)完成

(5)查看IDEA Console中Maven项目是否建立成功[INFO]----------------------------------------------------------------------------

[INFO]Usingfollowingparametersforcreatingprojectfrom

Archetype:maven-archetype-webapp:RELEASE

[INFO]----------------------------------------------------------------------------

[INFO]Parameter:groupId,Value:com.test

[INFO]Parameter:artifactId,Value:test

[INFO]Parameter:version,Value:1.0-SNAPSHOT

[INFO]Parameter:package,Value:com.test

[INFO]Parameter:packageInPathFormat,Value:com/test

[INFO]Parameter:package,Value:com.test

[INFO]Parameter:version,Value:1.0-SNAPSHOT

[INFO]Parameter:groupId,Value:com.test

[INFO]Parameter:artifactId,Value:test

[INFO]ProjectcreatedfromArchetypeindir:

C:\Users\Administrator\AppData\Local\Temp\archetypetmp\test

[INFO]------------------------------------------------------------------------

[INFO]BUILDSUCCESS

[INFO]------------------------------------------------------------------------

[INFO]Totaltime:13.736s

[INFO]Finishedat:-12-13T11:13:40+08:00

[INFO]FinalMemory:14M/150M

[INFO]------------------------------------------------------------------------

[INFO]Mavenexecutionfinished

(6)建立好的Maven项目树状结构图

2、整合SSM框架

(1)在Pom.xml中添加Spring、SpringMVC、MyBatis相关jar

UTF-8

1.7

1.7

3.2.0.RELEASE

3.2.7

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.2.2

javax.servlet

javax.servlet-api

3.0.1

provided

javax.servlet.jsp

jsp-api

2.2

provided

javax.servlet

jstl

1.2

taglibs

standard

1.1.2

com.fasterxml.jackson.core

jackson-databind

2.9.4

mysql

mysql-connector-java

5.1.39

commons-dbcp

commons-dbcp

1.2.2

commons-pool

commons-pool

1.3

ch.qos.logback

logback-classic

1.1.1

junit

junit

4.11

test

(2)在main/,新建java、resources文件夹,分别设为Sources Root和Resources Root,java主要是用来存放业务逻辑代码,resources主要是存放相关配置文件。

(3)在resources/,新建db.properties和log4j.properties,主要用于存放数据库的连接配置信息和日志配置信息。

db.properties:jdbc.driver=com.mysql.jdbc.Driver//使用的是mysqljdbc驱动

jdbc.url=jdbc:mysql://localhost:3306/database//url、port、database

jdbc.username=root//账号

jdbc.password=123456//密码

log4j.properties:#Globallogging

configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug

log4j.rootLogger=DEBUG,stdout

#Consoleoutput...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p[%t]-%m%n

(4)在mybatis/,创建mybatis整合需要的配置文件sqlMapConfig.xml

注:由于使用spring和mybatis的整合包进行mapper扫描,这里不需要进行mapper配置了。但必须遵循:mapper.xml和mapper.java文件同名且在一个目录。

(5)在spring/,创建spring整合需要的配置文件applicationContext.xml<?xml version="1.0"encoding="UTF-8"?>

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:context="/schema/context"

xmlns:aop="/schema/aop"

xmlns:tx="/schema/tx"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd

/schema/context

/schema/context/spring-context.xsd

/schema/aop

/schema/aop/spring-aop.xsd

/schema/tx

/schema/tx/spring-tx.xsd">

destroy-method="close">

DataSourceTransactionManager">

pointcut="execution(*cn.ssm.test.service.impl.*.*(..))"/>

(6)在spring/,创建springmvc.xml,配置注解映射器、注解适配器、视图解析器等<?xml version="1.0"encoding="UTF-8"?>

xmlns:xsi="/2001/XMLSchema-instance"

xmlns:util="/schema/util"

xmlns:mvc="/schema/mvc"

xmlns:context="/schema/context"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd

/schema/util

/schema/util/spring-util.xsd

/schema/mvc

/schema/mvc/spring-mvc.xsd

/schema/context

/schema/context/spring-context.xsd">

InternalResourceViewResolver">

FormattingConversionServiceFactoryBean">

CustomDateConverter"/>

(7)创建逆向工程,自动生成po及mapper文件

Po文件主要生成的是数据库表的字段定义,相关字段的Get和Set方法。

mapper文件主要生成的是数据库表中一般字段的查询接口及相关xml文件等。

po对象:publicclassItems{

privateIntegerid;

privateStringname;

privateFloatprice;

privateStringpic;

privateDatecreatetime;

privateStringdetail;

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name==null?null:name.trim();

}

publicFloatgetPrice(){

returnprice;

}

publicvoidsetPrice(Floatprice){

this.price=price;

}

publicStringgetPic(){

returnpic;

}

publicvoidsetPic(Stringpic){

this.pic=pic==null?null:pic.trim();

}

publicDategetCreatetime(){

returncreatetime;

}

publicvoidsetCreatetime(Datecreatetime){

this.createtime=createtime;

}

publicStringgetDetail(){

returndetail;

}

publicvoidsetDetail(Stringdetail){

this.detail=detail==null?null:detail.trim();

}

}

Mapper:

mapper接口类:publicinterfaceItemsMapper{

intcountByExample(ItemsExampleexample);

intdeleteByExample(ItemsExampleexample);

intdeleteByPrimaryKey(Integerid);

intinsert(Itemsrecord);

intinsertSelective(Itemsrecord);

ListselectByExampleWithBLOBs(ItemsExampleexample);

ListselectByExample(ItemsExampleexample);

ItemsselectByPrimaryKey(Integerid);

intupdateByExampleSelective(@Param("record")Itemsrecord,

@Param("example")ItemsExampleexample);

intupdateByExampleWithBLOBs(@Param("record")Itemsrecord,

@Param("example")ItemsExampleexample);

intupdateByExample(@Param("record")Itemsrecord,

@Param("example")ItemsExampleexample);

intupdateByPrimaryKeySelective(Itemsrecord);

intupdateByPrimaryKeyWithBLOBs(Itemsrecord);

intupdateByPrimaryKey(Itemsrecord);

}

Mapper xml文件:<?xml version="1.0"encoding="UTF-8"?>

mapperPUBLIC"-////DTDMapper3.0//EN"

"/dtd/mybatis-3-mapper.dtd">

extends="BaseResultMap">

and${criterion.condition}

and${criterion.condition}#{criterion.value}

and${criterion.condition}#{criterion.value}

and#{criterion.secondValue}

and${criterion.condition}

item="listItem"open="("close=")"separator=",">

#{listItem}

and${criterion.condition}

and${criterion.condition}#{criterion.value}

and${criterion.condition}#{criterion.value}

and#{criterion.secondValue}

and${criterion.condition}

item="listItem"open="("close=")"separator=",">

#{listItem}

id,name,price,pic,createtime

detail

parameterType="cn.ssm.test.po.ItemsExample">

select

distinct

,

fromitems

orderby${orderByClause}

parameterType="cn.ssm.test.po.ItemsExample">

select

distinct

fromitems

orderby${orderByClause}

parameterType="java.lang.Integer">

select

,

fromitems

whereid=#{id,jdbcType=INTEGER}

deletefromitems

whereid=#{id,jdbcType=INTEGER}

deletefromitems

insertintoitems(id,name,price,

pic,createtime,detail

)

values(#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},

#{price,jdbcType=REAL},

#{pic,jdbcType=VARCHAR},#{createtime,jdbcType=TIMESTAMP},

#{detail,jdbcType=LONGVARCHAR}

)

insertintoitems

id,

name,

price,

pic,

createtime,

detail,

#{id,jdbcType=INTEGER},

#{name,jdbcType=VARCHAR},

#{price,jdbcType=REAL},

#{pic,jdbcType=VARCHAR},

#{createtime,jdbcType=TIMESTAMP},

#{detail,jdbcType=LONGVARCHAR},

resultType="java.lang.Integer">

selectcount(*)fromitems

updateitems

id=#{record.id,jdbcType=INTEGER},

name=#{record.name,jdbcType=VARCHAR},

price=#{record.price,jdbcType=REAL},

pic=#{record.pic,jdbcType=VARCHAR},

createtime=#{record.createtime,jdbcType=TIMESTAMP},

detail=#{record.detail,jdbcType=LONGVARCHAR},

updateitems

setid=#{record.id,jdbcType=INTEGER},

name=#{record.name,jdbcType=VARCHAR},

price=#{record.price,jdbcType=REAL},

pic=#{record.pic,jdbcType=VARCHAR},

createtime=#{record.createtime,jdbcType=TIMESTAMP},

detail=#{record.detail,jdbcType=LONGVARCHAR}

updateitems

setid=#{record.id,jdbcType=INTEGER},

name=#{record.name,jdbcType=VARCHAR},

price=#{record.price,jdbcType=REAL},

pic=#{record.pic,jdbcType=VARCHAR},

createtime=#{record.createtime,jdbcType=TIMESTAMP}

updateitems

name=#{name,jdbcType=VARCHAR},

price=#{price,jdbcType=REAL},

pic=#{pic,jdbcType=VARCHAR},

createtime=#{createtime,jdbcType=TIMESTAMP},

detail=#{detail,jdbcType=LONGVARCHAR},

whereid=#{id,jdbcType=INTEGER}

updateitems

setname=#{name,jdbcType=VARCHAR},

price=#{price,jdbcType=REAL},

pic=#{pic,jdbcType=VARCHAR},

createtime=#{createtime,jdbcType=TIMESTAMP},

detail=#{detail,jdbcType=LONGVARCHAR}

whereid=#{id,jdbcType=INTEGER}

updateitems

setname=#{name,jdbcType=VARCHAR},

price=#{price,jdbcType=REAL},

pic=#{pic,jdbcType=VARCHAR},

createtime=#{createtime,jdbcType=TIMESTAMP}

whereid=#{id,jdbcType=INTEGER}

(8)在service文件夹内,通过使用service整合mapper接口,实现相关查询功能@Autowired

privateItemsMapperitemsMapper;

@Override

publicListfindItemsList(ItemsQueryVoitemsQueryVo)

throwsException{

//通过ItemsMapperCustom查询数据库

returnitemsMapperCustom.findItemsList(itemsQueryVo);

}

(9)创建controller,使用注解功能@Controller

//为了对url进行分类管理,可以在这里定义根路径,最终访问url是根路径+子路径

//比如:商品列表:/items/queryItems.action

@RequestMapping("/items")

publicclassItemsController{

@Autowired

privateItemsServiceitemsService;

//商品查询

@RequestMapping("/queryItems")

publicModelAndViewqueryItems(HttpServletRequestrequest)throwsException{

//测试forward后request是否可以共享

System.out.println(request.getParameter("id"));

//调用service查找数据库,查询商品列表

ListitemsList=itemsService.findItemsList(null);

//返回ModelAndView

ModelAndViewmodelAndView=newModelAndView();

//相当于request的setAttribut,在jsp页面中通过itemsList取数据

modelAndView.addObject("itemsList",itemsList);

//指定视图

//下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为

//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

//上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀

modelAndView.setViewName("items/itemsList");

returnmodelAndView;

}

}

(10)在WEB-INF下添加jsp文件,在jsp文件夹内添加html文件

(11)修改web.xml的配置,修改访问路径、配置外路径等。

xmlns="/xml/ns/javaee"

xsi:schemaLocation="/xml/ns/javaee

/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID"version="2.5">

test

contextConfigLocation

classpath:spring/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

spring_mybatis

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

spring_mybatis

*.action

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

3、实现Tomcat热部署

(1)找到Edit configurations,进入到如下界面

(2)点击fix或者点击Deployment,进入如下界面设置相关参数

(3)更改下图中的两项参数

(4)Build项目

(5)build后的结果展示

4、注意事项

(1)需要在pom.xml中的build下添加如下代码,解决编译后的mapper文件内无法编译XXXmapper.xml内容

src/main/java

**/*.xml

**/*.properties

false

(2)热部署的时候,注意不要改变Application context的默认配置,需要修改首页显示的页面,路径可在Server中配置

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。