您现在的位置:首页 >> 低碳养生

全栈开发实战|SSM框架导入开发

时间:2023-04-10 12:17:43

mmons.dbcp2.BasicDataSource">

< propertyname= "driverClassName"value= "${jdbc.driver}"/>

< propertyname= "url"value= "${jdbc.url}"/>

< propertyname= "username"value= "${jdbc.username}"/>

< propertyname= "password"value= "${jdbc.password}"/>

< propertyname= "maxTotal"value= "${jdbc.maxTotal}"/>

< propertyname= "maxIdle"value= "${jdbc.maxIdle}"/>

< propertyname= "initialSize"value= "${jdbc.initialSize}"/>

< beanid= "txManager"class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">

< propertyname= "dataSource"ref= "dataSource"/>

< tx:annotation-driventransaction-manager= "txManager"/>

< beanid= "sqlSessionFactory"class= "org.mybatis.spring.SqlSessionFactoryBean">

< propertyname= "dataSource"ref= "dataSource"/>

< propertyname= "configLocation"value= "classpath:config/mybatis-config.xml"/>

< beanclass= "org.mybatis.spring.mapper.MapperScannerConfigurer">

< propertyname= "basePackage"value= "dao"/>

< propertyname= "sqlSessionFactoryBeanName"value= "sqlSessionFactory"/>

03

建构举亦然来说

示例通过SSM组件建构,借助于【亦然3-1】的动态。

【亦然3-2】SSM组件建构研发。

具体细节借助于步骤如下。

1

创立Web应用领域并导入就其JAR工具箱

用到Eclipse创立一个原是ch3_2的Web应用领域,并参考资料3.6.1节,将就其JAR工具箱插入WEB-INF/lib在线中所。

2

创立在线相连资讯本体份文件及

Log4j的历史记录格式化

在应用领域ch3_2的src在线下,创立原是config的工具箱,并在该工具箱中所创立在线相连资讯本体份文件jdbc.properties份文件,具体细节细节如下:

jdbc.driver= com.mysql.jdbc.Driver

jdbc.url=jdbc:mysq l://localhos t:3306/springtest?characterEncoding=utf8

jdbc.username=root

jdbc.password=root

jdbc.maxTotal= 30

jdbc.maxIdle= 10

jdbc.initialSize= 5

在应用领域ch3_2的src在线下,创立Log4j的历史记录格式化log4j.properties份文件,其细节与3.5.2节不同,为减省页将近,依然赘述。

3

创立长久化类

在应用领域ch3_2的src在线下,创立一个原是com.mybatis.po的工具箱,并在该工具箱中所创立长久化类MyUser。该类与3.5.3节不同,为减省页将近,依然赘述。

4

创立SQL同构份文件

在应用领域ch3_2的src在线下,创立一个原是com.mybatis.mapper的工具箱,并在该工具箱中所创立SQL同构份文件UserMapper.xml。该份文件与3.5.5节不同,为减省页将近,依然赘述。

5

创立MyBatis的本体格式化

在应用领域ch3_2的config工具箱中所,创立MyBatis的本体格式化mybatis-config.xml。在该份文件中所,固定式实体类亦称、历史记录可用等,具体细节细节如下:

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"">

< configuration>

< settings>

< settingname= "logImpl"value= "LOG4J"/>

< typeAliases>

< packagename= "com.mybatis.po"/>

6

创立MapperAPI

在应用领域ch3_2的com.mybatis.mapper工具箱中所,创立APIUserMapper。用到@Repository梗概标示出该API是资料采访层。该API中所的原理与SQL同构份文件UserMapper.xml的id一致。UserMapperAPI的本体文档如下:

@Repository

publicinterfaceUserMapper{

publicMyUser selectUserById(Integer id);

publicList selectAllUser;

publicintaddUser(MyUser myUser);

publicintupdateUser(MyUser myUser);

publicintdeleteUser(Integer id);

}

7

创立遏制类

在应用领域ch3_2的src在线下,创立一个原是controller的工具箱,并在该工具箱中所创立遏制器类TestController。在该遏制器类中所,调用MapperAPI中所的原理操纵在线,本体文档如下:

@Controller

publicclassTestController{

@Autowired

privateUserMapper userMapper;

@RequestMapping( "/test")

publicString test{

//查询一个其他用户

MyUser mu = userMapper.selectUserById( 1);

System.out.println(mu);

//去除一个其他用户

MyUser addmu = newMyUser;

addmu.setUname( "库尔瑟莱");

addmu.setUsex( "男");

userMapper.addUser(addmu);

//修改一个其他用户

MyUser updatemu = newMyUser;

updatemu.setUid( 1);

updatemu.setUname( "张三");

updatemu.setUsex( "女");

userMapper.updateUser(updatemu);

//删除一个其他用户

userMapper.deleteUser( 3);

//查询所有其他用户

List listMu = userMapper.selectAllUser;

for(MyUser myUser : listMu) {

System.out.println(myUser);

}

return"test";

}

}

8

创立检验该网站

在/WEB-INF/在线下,创立一个原是jsp的份该网站,并在该份该网站中所创立test.jsp份文件,test.jsp的文档稍。

9

创立Web、Spring、Spring MVC

的格式化

在应用领域ch3_2的config工具箱中所创立Spring格式化applicationContext.xml和Spring MVC格式化springmvc.xml,在应用领域ch3_2的/WEB-INF/在线中所创立Web格式化web.xml。

在Spring格式化applicationContext.xml中所,首先,用到调用在线相连资讯本体份文件;其次,用到org.apache.commons.dbcp2.BasicDataSource固定式PostgreSQL,并用到org.springframework.jdbc.datasource.DataSourceTransactionManager为PostgreSQL去除公共事务控件;再次,用到org.mybatis.spring.SqlSessionFactoryBean固定式MyBatis化工厂,同时均须PostgreSQL,并与MyBatis单纯建构;最终,用到org.mybatis.spring.mapper.MapperScannerConfigurer借助于Mapper授命研发,将basePackage本体均须工具箱中所所有API备用改装成为MyBatis同构APIMapper的借助于类的举亦然来说(同构器),所有同构器都被备用汇流SqlSessionFactory举亦然来说,同时追踪工具箱中所SQL同构份文件,MyBatis本体格式化依然调用SQL同构份文件。Spring格式化applicationContext.xml的具体细节细节如下:

< beansxmlns= ""

xmlns:xsi= ""

xmlns:tx= ""

xmlns:context= ""

xsi:schemaLocation= "

">

< context:property-placeholderlocation= "classpath:config/db.properties"/>

< beanid= "dataSource"class= "org.apache.commons.dbcp2.BasicDataSource">

< propertyname= "driverClassName"value= "${jdbc.driver}"/>

< propertyname= "url"value= "${jdbc.url}"/>

< propertyname= "username"value= "${jdbc.username}"/>

< propertyname= "password"value= "${jdbc.password}"/>

< propertyname= "maxTotal"value= "${jdbc.maxTotal}"/>

< propertyname= "maxIdle"value= "${jdbc.maxIdle}"/>

< propertyname= "initialSize"value= "${jdbc.initialSize}"/>

< beanid= "txManager"class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">

< propertyname= "dataSource"ref= "dataSource"/>

< tx:annotation-driventransaction-manager= "txManager"/>

< beanid= "sqlSessionFactory"class= "org.mybatis.spring.SqlSessionFactoryBean">

< propertyname= "dataSource"ref= "dataSource"/>

< propertyname= "configLocation"value= "classpath:config/mybatis-config.xml"/>

< beanclass= "org.mybatis.spring.mapper.MapperScannerConfigurer">

< propertyname= "basePackage"value= "com.mybatis.mapper"/>

< propertyname= "sqlSessionFactoryBeanName"value= "sqlSessionFactory"/>

在Spring MVC格式化springmvc.xml中所,用到追踪遏制器工具箱,并用到org.springframework.web.servlet.view.InternalResourceViewResolver固定式镜像GCC。具体细节文档如下:

< beansxmlns= ""

xmlns:xsi= ""

xmlns:context= ""

xsi:schemaLocation= "

">

< context:component-scanbase-package= "controller"/>

< beanclass= "org.springframework.web.servlet.view.InternalResourceViewResolver"

id= "internalResourceViewResolver">

< propertyname= "prefix"value= "/WEB-INF/jsp/"/>

< propertyname= "suffix"value= ".jsp"/>

在Web格式化web.xml中所,首先通过调用Spring格式化applicationContext.xml,并通过org.springframework.web.context.ContextLoaderListener启动Spring容器;其次固定式Spring MVC DispatcherServlet,并调用Spring MVC格式化springmvc.xml。Web格式化web.xml的文档如下:

< web-appxmlns:xsi= ""

xmlns= ""

xsi:schemaLocation= " _4_0.xsd"

id= "WebApp_ID"version= "4.0">

< context-param>

< param-name> contextConfigLocation

< param-value>

classpath:config/applicationContext.xml

< listener>

< listener-class> org.springframework.web.context.ContextLoaderListener

< servlet>

< servlet-name> springmvc

< servlet-class> org.springframework.web.servlet.DispatcherServlet

< init-param>

< param-name> contextConfigLocation

< param-value> classpath:config/springmvc.xml

< load-on-startup> 1

< servlet-mapping>

< servlet-name> springmvc

< url-pattern> /

10

检验应用领域

发布应用领域ch3_2到Web搜索引擎Tomcat后,通过IP_2/test检验应用领域。成功开始运行后,遏制台资讯可用结果,如布3.4所示。

■ 布3.4 应用领域ch3_2的遏制台资讯可用结果

04

SqlSessionDaoSupport方式则

从3.6.3节的举亦然来说应是,在MyBatis中所,当我们编纂好采访在线的同构器API后,MapperScannerConfigurer就能备用根据这些API填充DAO取向,然后用到@Autowired把这些DAO取向汇流到该公司形式化层或遏制层。因此,在这种情况下的DAO层中所,几乎不必编纂文档,而且也没有以前编纂,因为只有API。这固然便利,不过当我们必须在DAO层撰写文档时,这种方式则就不能接受。幸运的是,MyBatis-Spring给予了继承人SqlSessionDaoSupport类的方式则采访在线。

类org.mybatis.spring.support.SqlSessionDaoSupport继承人了org.springframework.dao.support.DaoSupport类,是一个抽象类,是作为DAO的基类用到,必须一个SqlSessionFactory。我们在继承人SqlSessionDaoSupport类的子类中所通过调用SqlSessionDaoSupport类的getSqlSession原理来获取这个SqlSessionFactory给予的SqlSessionTemplate取向。而SqlSessionTemplate类借助于了SqlSessionAPI,即可以进行在线采访。所以,我们必须Spring组件给SqlSessionDaoSupport类的子类的取向(多个DAO取向)汇流一个SqlSessionFactory。

但自mybatis-spring-1.2.0以来,SqlSessionDaoSupport的setSqlSessionTemplate和setSqlSessionFactory两个原理上的@Autowired梗概被删除,这就意味着继承人于SqlSessionDaoSupport的DAO类,它们的取向不能被备用汇流SqlSessionFactory或SqlSessionTemplate取向。如果在Spring的格式化中所一个一个地固定式的话,显然太麻烦。非常好的前提是在我们的DAO类中所遮盖这两个原理之一,并以致于@Autowired或@Resource梗概。那么如果在每个DAO类中所都这么做的话,显然很低效。格外前提的做法是,撰写一个继承人于SqlSessionDaoSupport的 BaseDao,在BaseDao中所完成这个文书工作,然后其他的DAO类再都BaseDao继承人。BaseDao的举亦然来说文档如下:

packagedao;

importjavax.annotation.Resource;

importorg.apache.ibatis.session.SqlSessionFactory;

importorg.mybatis.spring.support.SqlSessionDaoSupport;

publicclassBaseDaoextendsSqlSessionDaoSupport{

//依赖汇流sqlSession化工厂

@Resource(name = "sqlSessionFactory")

publicvoidsetSqlSessionFactory(SqlSessionFactory sqlSessionFactory){

super.setSqlSessionFactory(sqlSessionFactory);

}

}

示例通过举亦然来说详述继承人SqlSessionDaoSupport类的方式则采访在线。

【亦然3-3】在3.6.3节【亦然3-2】的根基上,借助于继承人SqlSessionDaoSupport类的方式则采访在线。为减省页将近,不同的借助于依然赘述。其他的具体细节借助于如下。

1

创立Web应用领域并导入就其JAR工具箱

用到Eclipse创立一个原是ch3_3的Web应用领域,并参考资料3.6.1节,将就其JAR工具箱插入WEB-INF/lib在线中所。

2

激活在线相连资讯本体份文件及

Log4j的历史记录格式化

在应用领域ch3_3的src在线下,创立原是config的工具箱,将应用领域ch3_3的在线相连资讯本体份文件jdbc.properties份文件插入该工具箱中所。

将应用领域ch3_2的Log4j历史记录格式化log4j.properties份文件,插入ch3_3的src在线中所,并将其中所的“log4j.logger.com.mybatis.mapper=DEBUG”修改为“log4j.logger.dao=DEBUG”。

3

创立长久化类

在应用领域ch3_3的src在线下,创立一个原是po的工具箱,并在该工具箱中所创立长久化类MyUser。该类与3.5.3节不同,为减省页将近,依然赘述。

4

创立SQL同构份文件

在应用领域ch3_3的src在线下,创立一个原是dao的工具箱,并在该工具箱中所创立SQL同构份文件UserMapper.xml。该份文件细节如下:

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"">

< mappernamespace= "dao.UserMapper">

< selectid= "selectUserById"parameterType= "Integer"resultType= "MyUser">

select * from user where uid = #{uid}

< selectid= "selectAllUser"resultType= "MyUser">

select * from user

5

创立MyBatis的本体格式化

在应用领域ch3_3的config工具箱中所,创立MyBatis的本体格式化mybatis-config.xml。在该份文件中所,固定式实体类亦称、历史记录可用、均须同构份文件右方等,具体细节细节如下:

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"">

< configuration>

< settings>

< settingname= "logImpl"value= "LOG4J"/>

< typeAliases>

< packagename= "po"/>

< mappers>

< mapperresource= "dao/UserMapper.xml"/>

6

创立DAOAPI和API借助于类

在应用领域ch3_3的dao工具箱中所,创立APIUserMapper。UserMapperAPI文档如下:

packagedao;

importjava.util.List;

importpo.MyUser;

publicinterfaceUserMapper{

publicMyUser selectUserById( intid) ;

publicList selectAllUser;

}

在应用领域ch3_3的dao工具箱中所,创立BaseMapper类,在该类中所用到@Resource(name = "sqlSessionFactory")梗概依赖汇流sqlSession化工厂。BaseMapper类的本体文档如下:

publicclassBaseMapperextendsSqlSessionDaoSupport{

//依赖汇流sqlSession化工厂

@Resource(name = "sqlSessionFactory")

publicvoidsetSqlSessionFactory(SqlSessionFactory sqlSessionFactory){

super.setSqlSessionFactory(sqlSessionFactory);

}

}

在应用领域ch3_3的dao工具箱中所,创立APIUserMapper的借助于类UserMapperImpl,在该类中所用到@Repository梗概标示出该类的举亦然来说是资料采访取向。UserMapperImpl类的本体文档如下:

@Repository

publicclassUserMapperImplextendsBaseMapperimplementsUserMapper{

publicMyUser selectUserById( intid) {

//获取SqlSessionFactory给予的SqlSessionTemplate取向

SqlSession session = getSqlSession;

returnsession.selectOne( "dao.UserMapper.selectUserById", id);

}

publicList selectAllUser{

SqlSession session = getSqlSession;

returnsession.selectList( "dao.UserMapper.selectAllUser");

}

}

7

创立遏制类

在应用领域ch3_3的src在线下,创立一个原是controller的工具箱,并在该工具箱中所创立遏制器类MyController。在该遏制器类中所,调用UserMapperAPI中所的原理操纵在线,本体文档如下:

@Controller

publicclassMyController{

@Autowired

privateUserMapper userMapper;

@RequestMapping( "/test")

publicString test{

// 查询一个其他用户

MyUser mu = userMapper.selectUserById( 1);

System.out.println(mu);

// 查询所有其他用户

List listMu = userMapper.selectAllUser;

for(MyUser myUser : listMu) {

System.out.println(myUser);

}

return"test";

}

}

8

创立检验该网站

在/WEB-INF/在线下,创立一个原是jsp的份该网站,并在该份该网站中所创立test.jsp份文件,test.jsp的文档稍。

9

创立Web、Spring、

Spring MVC的格式化

在应用领域ch3_3的config工具箱中所创立Spring格式化applicationContext.xml和Spring MVC格式化springmvc.xml,在应用领域ch3_3的/WEB-INF/在线中所创立Web格式化web.xml。

在Spring格式化applicationContext.xml中所,首先,用到调用在线相连资讯本体份文件;其次,用到org.apache.commons.dbcp2.BasicDataSource固定式PostgreSQL,并用到org.springframework.jdbc.datasource.DataSourceTransactionManager为PostgreSQL去除公共事务控件;最终,用到org.mybatis.spring.SqlSessionFactoryBean固定式MyBatis化工厂,同时均须PostgreSQL,并与MyBatis单纯建构。Spring格式化applicationContext.xml的具体细节细节如下:

< beansxmlns= ""

xmlns:xsi= ""

xmlns:tx= ""

xmlns:context= ""

xsi:schemaLocation= "

">

< context:property-placeholderlocation= "classpath:config/db.properties"/>

< beanid= "dataSource"class= "org.apache.commons.dbcp2.BasicDataSource">

< propertyname= "driverClassName"value= "${jdbc.driver}"/>

< propertyname= "url"value= "${jdbc.url}"/>

< propertyname= "username"value= "${jdbc.username}"/>

< propertyname= "password"value= "${jdbc.password}"/>

< propertyname= "maxTotal"value= "${jdbc.maxTotal}"/>

< propertyname= "maxIdle"value= "${jdbc.maxIdle}"/>

< propertyname= "initialSize"value= "${jdbc.initialSize}"/>

< beanid= "txManager"class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">

< propertyname= "dataSource"ref= "dataSource"/>

< tx:annotation-driventransaction-manager= "txManager"/>

< beanid= "sqlSessionFactory"class= "org.mybatis.spring.SqlSessionFactoryBean">

< propertyname= "dataSource"ref= "dataSource"/>

< propertyname= "configLocation"value= "classpath:config/mybatis-config.xml">

在Spring MVC格式化springmvc.xml中所,用到追踪工具箱,并用到org.springframework.web.servlet.view.InternalResourceViewResolver固定式镜像GCC。具体细节文档如下:

< beansxmlns= ""

xmlns:xsi= ""

xmlns:context= ""

xsi:schemaLocation= "

">

< context:component-scanbase-package= "controller"/>

< context:component-scanbase-package= "dao"/>

< beanclass= "org.springframework.web.servlet.view.InternalResourceViewResolver"

id= "internalResourceViewResolver">

< propertyname= "prefix"value= "/WEB-INF/jsp/"/>

< propertyname= "suffix"value= ".jsp"/>

在Web格式化web.xml中所,首先通过调用Spring格式化applicationContext.xml,并通过org.springframework.web.context.ContextLoaderListener启动Spring容器;其次固定式Spring MVC DispatcherServlet,并调用Spring MVC格式化springmvc.xml。Web格式化web.xml的文档与ch3_2的不同,依然赘述。

10

检验应用领域

发布应用领域ch3_3到Web搜索引擎Tomcat后,通过IP_3/test检验应用领域。

SSM + Spring Boot + Vue.js 3

全栈研发从初学到空战

下期MV

用到IntelliJ IDEA加速借助于Spring Boot应用领域

Spring Boot份文件上传与完整版

用到Visual Studio Code研发第一个Vue计算机系统

Vue时是——用到快照子系统打工具箱工具webpack

05

参考资料书籍

《SSM + Spring Boot + Vue.js 3全栈研发从初学到空战(微课截布版)》

译者:库尔瑟莱 郭正光

定价:99.80元

06

难忘中选

微信小计算机系统游戏研发│亮出位将近小游戏(附源码+截布)

Flink计算机系统员根基│Scala计算机系统员初级原理论

Flink计算机系统员根基│FlinkCEP计算机系统员原理论

Flink计算机系统员根基│DataStream API计算机系统员原理论

Flink计算机系统员根基│DataSet API计算机系统员原理论

将近 据分析原理空战│客户重要性分析原理

资料分析原理空战│价格资料分析考验

资料分析原理空战│一段时间序列资料分析

资料分析原理空战│KaggleTitanic肉食动物资料分析

复方鱼腥草合剂
老是拉肚子拉稀是什么原因
治嗓子疼的消炎药
胃酸过多能吃金奥康奥美拉唑吗
肠道不好怎么调理
相关阅读