使用ssm框架编写简单图书管理系统(一)
1.创建项目 编写配置文件
可参考上一篇文章 https://blog.****.net/xyz1995920/article/details/79901324
贴上配置文件:
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.vp</groupId> <artifactId>BookManger</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>BookManger Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <!--配置德鲁伊的数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!--配置mybatis需要的依赖包--> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.17.1-GA</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!-- spring jar--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.ow2.asm/asm --> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>4.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> </dependencies> <build> <defaultGoal>compile</defaultGoal> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <finalName>BookManger</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <!--允许移动生成的文件--> <verbose>true</verbose> <!--允许覆盖生成的文件--> <overwrite>true</overwrite> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2、web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name> <!-- 启动spring监听器 --> <listener> <description>Spring</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、applicationContext.xml (spring配置文件)
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- 1.引入db.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.开启spring ioc注解及扫描 --> <context:annotation-config/> <context:component-scan base-package="cn.vp"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 编写mybatis的配置 --> <!-- 3.配置连接池 druid --> <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <property name="initialSize" value="5"/> <property name="maxActive" value="20"/> <property name="minIdle" value="2"/> <property name="maxWait" value="5000"/> </bean> <!-- 4 配置sqlsessionfactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="druidDataSource"/> <!-- 加载xml文件--> <property name="mapperLocations" value="classpath:cn/vp/dao/*.xml"/> <!-- 给类取别名mybayis--> <property name="typeAliasesPackage" value="cn.vp"/> </bean> <!-- 5.配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.vp.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 6.配置事务 --> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="druidDataSource"/> </bean> <!-- 开启事务的注解 --> <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/> </beans>4、springmvc-servlet.xml(springmvc配置文件)
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启扫描 --> <context:component-scan base-package="cn.vp.controller"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 开启spring mvc注解 --> <mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"/> <!--静态资源处理器 --> <mvc:resources location="/images/" mapping="/images/**"/> <!--全局日期转换器 进行注册 --> <bean id="formattingConversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean id="dateConverter" class="cn.vp.controller.DateConverter"/> </list> </property> </bean> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <!-- 配置前缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置后缀 --> <property name="suffix" value=".jsp"/> <!-- 使用jstl的配置 --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> <!-- 文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="defaultEncoding" value="utf-8"/> <!-- 默认编码 (ISO-8859-1) --> <property name="maxInMemorySize" value="10240"/> <!-- 最大内存大小 (10240)--> <property name="uploadTempDir" value="/images/"/> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) --> <property name="maxUploadSize" value="-1"/> <!-- 最大文件大小,-1为无限止(-1) --> </bean> <mvc:resources location="/images/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**"/> </beans>
5、generatorconfig.xml(自动生成javabean和dao)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://www.mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="jdbc.properties"/> <!-- 数据库驱动 改动1 --> <classPathEntry location="H:\Maven\LocalWarehouse\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 生成的pojo,将implements Serializable--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <commentGenerator> <property name="suppressDate" value="true" /> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true" /> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="cn.vp.bean" targetProject="src/main/java"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="true" /> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置 --> <sqlMapGenerator targetPackage="cn.vp.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.vp.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成哪些表 --> <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 --> <table tableName="book_info" domainObjectName="BookInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="book_type" domainObjectName="BookType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
6、jdbc.properties和log4j.properties
2. 生成代码并修改
1.配置generator
2.配置tomcat
3.生成代码
4.根据需求修改实体类代码
在BookInfoMapper类加上查询所有图书的方法
在对应的xml里面加上sql语句 并修改上面因为实体类变化要改的代码
<?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="cn.vp.dao.BookInfoMapper"> <resultMap id="BaseResultMap" type="cn.vp.bean.BookInfo"> <id column="book_id" jdbcType="INTEGER" property="bookId" /> <result column="book_code" jdbcType="VARCHAR" property="bookCode" /> <result column="book_name" jdbcType="VARCHAR" property="bookName" /> <result column="book_author" jdbcType="VARCHAR" property="bookAuthor" /> <result column="publish_press" jdbcType="VARCHAR" property="publishPress" /> <result column="publish_date" jdbcType="DATE" property="publishDate" /> <result column="is_borrow" jdbcType="INTEGER" property="isBorrow" /> <result column="path" jdbcType="VARCHAR" property="path" /> <association property="book_type" javaType="cn.vp.bean.BookType"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="type_name" jdbcType="VARCHAR" property="typeName" /> </association> </resultMap> <sql id="Base_Column_List"> book_id, book_code, book_name, book_type, book_author, publish_press, publish_date, is_borrow, path </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from book_info where book_id = #{bookId,jdbcType=INTEGER} </select> <select id="queryAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from book_info </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from book_info where book_id = #{bookId,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="cn.vp.bean.BookInfo"> insert into book_info (book_id, book_code, book_name, book_type, book_author, publish_press, publish_date, is_borrow, path ) values (#{bookId,jdbcType=INTEGER}, #{bookCode,jdbcType=VARCHAR}, #{bookName,jdbcType=VARCHAR}, #{bookType.id,jdbcType=INTEGER}, #{bookAuthor,jdbcType=VARCHAR}, #{publishPress,jdbcType=VARCHAR}, #{publishDate,jdbcType=DATE}, #{isBorrow,jdbcType=INTEGER}, #{path,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="cn.vp.bean.BookInfo"> insert into book_info <trim prefix="(" suffix=")" suffixOverrides=","> <if test="bookId != null"> book_id, </if> <if test="bookCode != null"> book_code, </if> <if test="bookName != null"> book_name, </if> <if test="bookType != null"> book_type, </if> <if test="bookAuthor != null"> book_author, </if> <if test="publishPress != null"> publish_press, </if> <if test="publishDate != null"> publish_date, </if> <if test="isBorrow != null"> is_borrow, </if> <if test="path != null"> path, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="bookId != null"> #{bookId,jdbcType=INTEGER}, </if> <if test="bookCode != null"> #{bookCode,jdbcType=VARCHAR}, </if> <if test="bookName != null"> #{bookName,jdbcType=VARCHAR}, </if> <if test="bookType != null"> #{bookType.id,jdbcType=INTEGER}, </if> <if test="bookAuthor != null"> #{bookAuthor,jdbcType=VARCHAR}, </if> <if test="publishPress != null"> #{publishPress,jdbcType=VARCHAR}, </if> <if test="publishDate != null"> #{publishDate,jdbcType=DATE}, </if> <if test="isBorrow != null"> #{isBorrow,jdbcType=INTEGER}, </if> <if test="path != null"> #{path,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="cn.vp.bean.BookInfo"> update book_info <set> <if test="bookCode != null"> book_code = #{bookCode,jdbcType=VARCHAR}, </if> <if test="bookName != null"> book_name = #{bookName,jdbcType=VARCHAR}, </if> <if test="bookType != null"> book_type = #{bookType.id,jdbcType=INTEGER}, </if> <if test="bookAuthor != null"> book_author = #{bookAuthor,jdbcType=VARCHAR}, </if> <if test="publishPress != null"> publish_press = #{publishPress,jdbcType=VARCHAR}, </if> <if test="publishDate != null"> publish_date = #{publishDate,jdbcType=DATE}, </if> <if test="isBorrow != null"> is_borrow = #{isBorrow,jdbcType=INTEGER}, </if> <if test="path != null"> path = #{path,jdbcType=VARCHAR}, </if> </set> where book_id = #{bookId,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="cn.vp.bean.BookInfo"> update book_info set book_code = #{bookCode,jdbcType=VARCHAR}, book_name = #{bookName,jdbcType=VARCHAR}, book_type = #{bookType.id,jdbcType=INTEGER}, book_author = #{bookAuthor,jdbcType=VARCHAR}, publish_press = #{publishPress,jdbcType=VARCHAR}, publish_date = #{publishDate,jdbcType=DATE}, is_borrow = #{isBorrow,jdbcType=INTEGER}, path = #{path,jdbcType=VARCHAR} where book_id = #{bookId,jdbcType=INTEGER} </update> </mapper>
到目前为止框架大致搭完