ssmbuild

配置文件

  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--应用spring总配置文件applicationContext.xml-->
    <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:applicationContext.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>


    <!--过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--在web.xml中的会话配置。15分钟后失效-->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    
</web-app>
  • db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=123
  • mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--pojo-->
    <typeAliases>
        <package name="com.adair.pojo"/>
    </typeAliases>


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

    <!--引入database.properties-->
    <context:property-placeholder location="classpath:database.properties"/>

<!--    <bean id="data" class="org.springframework.jdbc.datasource.DriverManagerDataSource"/>-->

    <!--连接池eg:c3p0自动化、dbcp半自动化、druid、hikari、-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- c3p0连接池的私有属性 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>


    <!--sqlSessionFactory注入datasource,mybatis-config.xml-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!--配置dao接口扫描包,动态的实现Dao可以注入到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory。相当于之前的BookMapperImpl-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--扫描dao-->
        <property name="basePackage" value="com.adair.dao"/>
    </bean>

</beans>
  • spring-service.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--1、扫描service层-->
    <context:component-scan base-package="com.adair.service"/>

    <!--2、将所有的业务层,注入到spring。BookServiceImpl进行注入,也可注解配置-->
    <bean id="BookServiceImpl" class="com.adair.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <!--3、声命事务配置-->
    <bean id="DataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>
  • spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描包-->
    <context:component-scan base-package="com.adair.controller"/>
    <!--注解驱动-->
    <mvc:annotation-driven/>
    <!--静态资源过滤-->
    <mvc:default-servlet-handler/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

java

  • BooksDao
public interface BooksDao {
/*增删改查*/
    @Insert("insert into books (bookName,bookCounts,detail) values (#{bookName},#{bookCounts},#{detail})")
    int addBooks(Books books);

    @Delete("delete from books where bookID=#{bookID}")
    int deleteBooks(int bookID);

    @Update("update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID}")
    int updateBooks(Books books);

    @Select("select * from books where bookID=#{bookID}")
    Books queryBooksById(int bookID);

    @Select("select * from books where bookName=#{bookName}")
    Books queryBooksByName(String bookName);

    @Select("select * from books")
    List<Books> queryAllBooks();
}
  • controller
@Controller
public class BooksController {
    @Autowired
    @Qualifier("booksServiceImpl")
    private BooksService booksService;

    @RequestMapping("/allBooks")
    public String allBooks(Model model){
        List<Books> list = booksService.queryAllBooks();
        model.addAttribute("list",list);
        return "allBooks";
    }
    @RequestMapping("/toupdateBooks/{Id}")
    public String updateBooks(@PathVariable("Id")int id,Model model){
        Books books = booksService.queryBooksById(id);
        model.addAttribute("books",books);
        return "updateBooks";
    }

    @RequestMapping("/updateBooks")
    public String updateBooks(Books books) {
        booksService.updateBooks(books);
        return "redirect:/allBooks";
    }
    @RequestMapping("/deleteBooks/{Id}")
    public String updateBooks(@PathVariable("Id")int id) {
        booksService.deleteBooks(id);
        return "redirect:/allBooks";
    }
    @RequestMapping("/queryBooksByName")
    public String queryBooksByName(String bookName,Model model){
        Books books = booksService.queryBooksByName(bookName);
        List<Books> list = new ArrayList<>();
        if(books == null){
            list = booksService.queryAllBooks();
            model.addAttribute("error","未查到");
        }
        list.add(books);
        model.addAttribute("list",list);
        return "allBooks";
    }

    @RequestMapping("/toaddBooks")
    public String toaddBooks() {
        return "addBooks";
    }
    @RequestMapping("/addBooks")
    public String addBooks(Books books) {
        booksService.addBooks(books);
        return "redirect:/allBooks";
    }
}

Web

  • index.jsp
<a href="${pageContext.request.contextPath}/allBooks">全部书籍</a>
  • allBooks.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="${pageContext.request.contextPath}/queryBooksByName">
    <span style="color: red">${error}</span>
    <input type="text" placeholder="query" name="bookName">
    <input type="submit">
</form>

<a href="${pageContext.request.contextPath}/toaddBooks">新增书籍</a>

<table>
    <thead>
        <th>书籍序列</th>
        <th>书籍名称</th>
        <th>书籍数量</th>
        <th>书籍描述</th>
        <th>书籍操作</th>
    </thead>

    <tbody>
        <c:forEach var="book" items="${list}">
            <tr>
                <td>${book.bookID}</td>
                <td>${book.bookName}</td>
                <td>${book.bookCounts}</td>
                <td>${book.detail}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/toupdateBooks/${book.bookID}">改变</a>
                    &nbsp;|&nbsp;
                    <a href="${pageContext.request.contextPath}/deleteBooks/${book.bookID}">删除</a>
                </td>
            </tr>
        </c:forEach>
    </tbody>
</table>


</body>
</html>
  • addBooks.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="${pageContext.request.contextPath}/addBooks" method="get">
    <label>书籍名字</label>
    <input type="text" name="bookName" required>
    <label>书籍数量</label>
    <input type="text" name="bookCounts" required>
    <label>书籍描述</label>
    <input type="text" name="detail"  required>

    <input type="submit">
</form>

</body>
</html>
  • updateBooks.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="${pageContext.request.contextPath}/updateBooks" method="get">
    <input type="hidden" name="bookID" value="${books.bookID}">
    <label>书籍名字</label>
    <input type="text" name="bookName" value="${books.bookName}" required>
    <label>书籍数量</label>
    <input type="text" name="bookCounts" value="${books.bookCounts}" required>
    <label>书籍描述</label>
    <input type="text" name="detail" value="${books.detail}" required>

    <input type="submit">
</form>


</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值