【Spring】作业记录:spring项目从创建、配置到功能实现、测试
提前声明:
1.这只是文档一次作业记录,也许会有不太恰当的地方,所以仅供参考。
2.适合不知道怎么创建配置的参考。仅仅是参考,而不是抄代码。
目录
- 这是一个Spring整合MyBatis的第一次尝试,对于文件的命名和存放位置,也许有些不太合理,请见谅
- 需要数据库的请查阅【Mybatis】学习笔记01:连接数据库,实现增删改 - 萌狼蓝天
- 本文档使用的SQL语句是MyBatis动态SQL语句(Choose)
项目创建
配置pom.xml
<dependencies>
<!-- 数据库驱动:MySQL-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- 数据库框架:mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!-- Spring框架:Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- Spring JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.17</version>
</dependency>
<!-- Spring整合Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!-- Spring AOP框架-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9</version>
</dependency>
<!-- 日志:log4g-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!-- 测试:junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency><!-- 分页插件-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
<!-- Json序列化工具:fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
连接数据库
快速创建实例化对象
选择目录
创建完毕后需要去修改它们的包名
配置
目录
database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/edu_smbms
jdbc.username=edu_smbms
jdbc.password=**********
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?><!--XML文件声明,版本号,编码-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"><!--该XML文件约束是dtd文件规则,和Spring中的约束不一样,但是功能一样-->
<configuration>
<settings>
<!-- 开启Log4j日志-->
<setting name="logImpl" value="LOG4J"/>
<!-- 开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<typeAliases>
<!-- <typeAlias type="" alias=""/>-->
<package name="cc.mllt.pojo"/><!--如果jdbc包下有Link类,则别名为link,不区分大小写-->
</typeAliases>
<mappers>
<package name="cc.mllt.mapper"/>
</mappers>
</configuration>
Spring-dao.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--引入数据库配置文件-->
<context:property-placeholder location="classpath:database.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定数据源-->
<property name="dataSource" ref="dataSource"/>
<!--指定Mybatis全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- <property name="mapperLocations" value="classpath:cc/mllt/mapper/*.xml"/>-->
</bean>
<!--配置mapper扫描,动态实现了mapper接口可以注册到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的mapper -->
<property name="basePackage" value="cc.mllt.mapper"/>
</bean>
<!--sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造器注入SQLSessionFactory。因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="info" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--启用spring的一些annotation -->
<context:annotation-config/>
<import resource="spring-dao.xml"/>
<!-- <import resource="spring-mvc.xml"/>-->
<bean id="providerMapperBean" class="cc.mllt.impl.ProviderImpl">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
</beans>
接口、映射、实现类
ProviderMapper.java
package cc.mllt.mapper;
import cc.mllt.pojo.SmbmsProvider;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ProviderMapper {
/**
* 查询供应商信息
* @param way 查询方式。1:根据供应商公司名称模糊查询 2:根据供应商负责人名字模糊查询 3:根据电话模糊查询
* @param npc 查询的关键字
* @return Provider
*/
public List<SmbmsProvider> getProviderByNCP(@Param("way") int way, @Param("npc") String npc);
}
ProviderMapper.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="cc.mllt.mapper.ProviderMapper">
<!-- public List<SmbmsProvider> getProviderByNCP(@Param("way") int way,@Param("npc") String npc);-->
<select id="getProviderByNCP" resultType="SmbmsProvider">
select * from smbms_provider
<choose>
<when test="way==1">
where proName like concat('%',#{npc},'%')
</when>
<when test="way==2">
where proContact like concat('%',#{npc},'%')
</when>
<when test="way==3">
where proPhone like concat('%',#{npc},'%')
</when>
<otherwise>
where proName like #concat('%',#{npc},'%')
</otherwise>
</choose>
</select>
</mapper>
ProviderImpl.java
package cc.mllt.impl;
import cc.mllt.mapper.ProviderMapper;
import cc.mllt.pojo.SmbmsProvider;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
//实现类
public class ProviderImpl implements ProviderMapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
this.sqlSessionTemplate=sqlSessionTemplate;
}
@Override
public List<SmbmsProvider> getProviderByNCP(int way, String npc) {
ProviderMapper providerMapper = sqlSessionTemplate.getMapper(ProviderMapper.class);
return providerMapper.getProviderByNCP(way,npc);
}
}
测试
@Test
public void getProviderByNCPTest(){
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
ProviderMapper providerMapper = context.getBean("providerMapperBean",ProviderMapper.class);
List<SmbmsProvider> result = providerMapper.getProviderByNCP(2,"王");
for(SmbmsProvider smbmsProvider:result){
System.out.println(smbmsProvider);
}
}
C:\Users\xrilang\.jdks\openjdk-17.0.2\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\SoftwareInstall\JetBrains\Toolbox\apps\IDEA-U\ch-1\221.5080.210\lib\idea_rt.jar=8953:D:\SoftwareInstall\JetBrains\Toolbox\apps\IDEA-U\ch-1\221.5080.210\bin -Dfile.encoding=UTF-8 -classpath D:\SoftwareInstall\JetBrains\Toolbox\apps\IDEA-U\ch-1\221.5080.210\lib\idea_rt.jar;D:\SoftwareInstall\JetBrains\Toolbox\apps\IDEA-U\ch-1\221.5080.210\plugins\junit\lib\junit5-rt.jar;D:\SoftwareInstall\JetBrains\Toolbox\apps\IDEA-U\ch-1\221.5080.210\plugins\junit\lib\junit-rt.jar;N:\JavaEE\MyStudyCode\pra-mybatis-smbms\target\test-classes;N:\JavaEE\MyStudyCode\pra-mybatis-smbms\target\classes;A:\100_env\framework\java\MyMavenJarLib\mysql\mysql-connector-java\8.0.28\mysql-connector-java-8.0.28.jar;A:\100_env\framework\java\MyMavenJarLib\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;A:\100_env\framework\java\MyMavenJarLib\org\mybatis\mybatis\3.5.9\mybatis-3.5.9.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-webmvc\5.3.18\spring-webmvc-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-aop\5.3.18\spring-aop-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-beans\5.3.18\spring-beans-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-context\5.3.18\spring-context-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-core\5.3.18\spring-core-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-jcl\5.3.18\spring-jcl-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-expression\5.3.18\spring-expression-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-web\5.3.18\spring-web-5.3.18.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-jdbc\5.3.17\spring-jdbc-5.3.17.jar;A:\100_env\framework\java\MyMavenJarLib\org\springframework\spring-tx\5.3.17\spring-tx-5.3.17.jar;A:\100_env\framework\java\MyMavenJarLib\org\mybatis\mybatis-spring\2.0.7\mybatis-spring-2.0.7.jar;A:\100_env\framework\java\MyMavenJarLib\org\aspectj\aspectjweaver\1.9.9\aspectjweaver-1.9.9.jar;A:\100_env\framework\java\MyMavenJarLib\log4j\log4j\1.2.12\log4j-1.2.12.jar;A:\100_env\framework\java\MyMavenJarLib\junit\junit\4.13.2\junit-4.13.2.jar;A:\100_env\framework\java\MyMavenJarLib\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;A:\100_env\framework\java\MyMavenJarLib\com\github\pagehelper\pagehelper\5.3.0\pagehelper-5.3.0.jar;A:\100_env\framework\java\MyMavenJarLib\com\github\jsqlparser\jsqlparser\4.2\jsqlparser-4.2.jar;A:\100_env\framework\java\MyMavenJarLib\com\alibaba\fastjson\2.0.1\fastjson-2.0.1.jar;A:\100_env\framework\java\MyMavenJarLib\com\alibaba\fastjson2\fastjson2-extension\2.0.1\fastjson2-extension-2.0.1.jar;A:\100_env\framework\java\MyMavenJarLib\com\alibaba\fastjson2\fastjson2\2.0.1\fastjson2-2.0.1.jar com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 MappperProviderTest,getProviderByNCPTest DEBUG 04-26 19:01:09,419 Creating a new SqlSession (Logger.java:49) DEBUG 04-26 19:01:09,423 SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e2ff723] was not registered for synchronization because synchronization is not active (Logger.java:49) DEBUG 04-26 19:01:10,882 JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@55b5e331] will not be managed by Spring (Logger.java:49) DEBUG 04-26 19:01:10,905 ==> Preparing: select * from smbms_provider where proContact like concat('%',?,'%') (BaseJdbcLogger.java:137) DEBUG 04-26 19:01:10,937 ==> Parameters: 王(String) (BaseJdbcLogger.java:137) DEBUG 04-26 19:01:11,054 <== Total: 3 (BaseJdbcLogger.java:137) DEBUG 04-26 19:01:11,055 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e2ff723] (Logger.java:49) SmbmsProvider{id=2, proCode='HB_GYS001', proName='石家庄帅益食品贸易有限公司', proDesc='长期合作伙伴,主营产品:饮料、水饮料、植物蛋白饮料、休闲食品、果汁饮料、功能饮料等', proContact='王军', proPhone='13309094212', proAddress='河北省石家庄新华区', proFax='0311-67738876', createdBy=1, creationDate=2016-04-13 04:20:40.0, modifyDate=null, modifyBy=0} SmbmsProvider{id=7, proCode='BJ_GYS003', proName='北京国粮食用油有限公司', proDesc='初次合作伙伴,主营产品:花生油、大豆油、小磨油等', proContact='王驰', proPhone='13344441135', proAddress='北京大兴青云店开发区', proFax='010-588134111', createdBy=1, creationDate=2016-04-13 00:00:00.0, modifyDate=null, modifyBy=0} SmbmsProvider{id=15, proCode='ZJ_GYS002', proName='乐摆日用品厂', proDesc='长期合作伙伴,主营产品:各种中、高档塑料杯,塑料乐扣水杯(密封杯)、保鲜杯(保鲜盒)、广告杯、礼品杯', proContact='王世杰', proPhone='13212331567', proAddress='浙江省金华市义乌市义东路', proFax='0579-34452321', createdBy=1, creationDate=2016-08-22 10:01:30.0, modifyDate=null, modifyBy=0} 进程已结束,退出代码0
版 权 声 明
作者:萌狼蓝天
QQ:3447902411(仅限技术交流,添加请说明方向)
转载请注明原文链接:https://www.cnblogs.com/mllt/p/log-homework-spring0base.html