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: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-4.3.xsd
	   http://www.springframework.org/schema/mvc
  	   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	   http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--Controller层注解扫描-->

    <context:component-scan base-package="com.lsnu.cross.controller" />
    <mvc:annotation-driven />
    <mvc:resources location="/static/" mapping="/static/**" />
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="1048576600"/>
    </bean>

    <mvc:interceptors>

        <bean class="com.lsnu.cross.interceptor.LoginInterceptor" />

        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.lsnu.cross.interceptor.Interceptor1"/>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <bean class="com.lsnu.cross.interceptor.Interceptor2"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

 applicationContext

<?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:tx="http://www.springframework.org/schema/tx"
       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/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxTotal" value="${jdbc.maxTotal}" />
        <property name="maxIdle" value="${jdbc.maxIdle}" />
        <property name="initialSize" value="${jdbc.initialSize}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lsnu.cross.mapper" />
    </bean>
    <context:component-scan base-package="com.lsnu.cross.service" />


</beans>

AuthorMapper

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lsnu.cross.mapper.AuthorMapper">
    <sql id="Base_Column_list">
        id,username,password,email,bio,favourite_section
    </sql>

    <delete id="deleteByPrimaryKey" parameterType="Integer">
        delete from author
        where id=#{id}
        
    </delete>

    <insert id="insertSelective" parameterType="Author">
      <selectKey keyProperty="id" order="AFTER" resultType="Integer">
    SELECT LAST_INSERT_ID()
</selectKey>
insert into author
values (#{username},#{password},#{email},#{bio},#{favouriteSection})
    </insert>

    <select id="selectByPrimaryKey" parameterType="Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from author
        where id = #{id}
    </select>

    <select id="getCount" resultType="Integer">
        select count(*) from  author
    </select>

    <select id="selectAuthors" resultType="Author">
        select * from from author
    </select>


    <insert id="insert" parameterType="Author">
        <selectKey keyProperty="id" order="AFTER" resultType="Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into author(username,password,email,bio,favouriteSection)
        value({#{username},#{password},#{email},#{bio},#{favouriteSection})
    </insert>

    <select id="selectByPrimaryKey" parameterType="Integer" >
select
<include refid="Base_Column_List"/>
from  author
where id=#{id}

    </select>

    <update id="updateByPrimaryKey" keyProperty="Author">
        update author
        set
            username=#{username},
            password=#{password},
            email=#{email},
            bio=#{bio},
            favoriteSection=#{favoriteSection},

        where id=#{id}

    </update>

    <update id="updateByPrimaryKeySelective" keyProperty="Author">
        update author
        <set>
            username=#{username}
            password=#{password}
            email=#{email}
            bio=#{bio}
            favoriteSection=#{favoriteSection}
        </set>
        where id=#{id}
    </update>

UserMapper

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lsnu.cross.mapper.UserMapper">
    <sql id="Base_Column_list">
        userId,userName,hashedPassword,email
    </sql>

    <insert id="insert" parameterType="User">
insert into users(userId,userName,hashedPassword,email)
values(#{userId},#{userName},#{hashedPassword},#{email})
    </insert>

    <insert id="insertSelective" parameterType="User">
insert  into users(userId,userName,hashedPassword,email)
values(#{userId},#{userName},#{hashedPassword},#{email})
    </insert>

    <select id="selectByPrimaryKey"  parameterType="User">
select
<include refid="Base_Column_list"></include>
from  User
where userId=#{userId}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="User">
delete from users
where userId=#{userId}

    </delete>

    <update id="updateByPrimaryKey" parameterType="User">
update users
set userName=#{userName},
hashedPassword=#{hashedPassword},
email=#{email}
where userId=#{userId}
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="User">
update users
set  userName=#{userName},
hashedPassword=#{hashedPassword},
email=#{email}
where userId=#{userId}
    </update>
</mapper>

AuthorService

package com.lsnu.cross.service;

import com.lsnu.cross.domain.Author;
import com.lsnu.cross.mapper.AuthorMapper;
import com.lsnu.cross.tools.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AuthorService {
    @Autowired
    private AuthorMapper authorMapper;

    public List<Author> findAll()
    {
        return authorMapper.selectAuthors();
    }

    public Page<Author> findPage(Integer pageNo, Integer pageSize)
    {
        int count = authorMapper.getCount();
        if (pageNo==null||pageNo<=1)
            pageNo=1;
        if(pageSize==null)
            pageSize=5;
        int pageCount=Integer.parseInt(String.valueOf(count/pageSize));
        if (pageNo>pageCount)
            pageNo=pageCount;
        Integer start=(pageNo-1)*pageSize;
        if(start<0)
            start=0;
        RowBounds rowBounds = new RowBounds(start,pageSize);
        List<Author> authors = authorMapper.selectAuthors(rowBounds);

        Page<Author> page = new Page<Author>();
        page.setPageNo(pageNo);
        page.setResults(authors);
        page.setPageSize(pageSize);
        page.setTotal(count);
        return page;
    }

    public Integer save(Author author)
    {
        return authorMapper.insert(author);
    }
}

add Jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/save" method="post">
    用户名:<input type="text" name="userName" /><br />
    密码:<input type="text" name="hashedPassword" /><br />
    邮箱:<input type="text" name="email" /><br />

    家庭地址:<input type="text" name="homeAddr" /><br />
    办公地址:<input type="text" name="officeAddr" /><br />
    家庭电话:<input type="text" name="homeTel" /><br />
    办公电话:<input type="text" name="officeTel" /><br />

    兴趣爱好:<input name="hobby" value="1" type="checkbox">音乐<br />
    <input name="hobby" value="2" type="checkbox">武术<br />
    <input name="hobby" value="3" type="checkbox">足球<br />
    <input name="hobby" value="4" type="checkbox">电竞<br />
    <input type="submit" value="提交"/>
</form>

</body>
</html>

用户名:${user.userName}<br/>
密码:${user.hashedPassword}<br/>
邮箱:${user.email}<br/>
家庭地址:${user.homeAddr}<br/>
办公地址:${user.officeAddr}<br/>
家庭电话:${user.homeTel}<br/>
办公电话:${user.officeTel}<br/>

pageList

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>author list</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap.css">
</head>
<body>
<table class="table table-bordered table-hover">
    <thead class="thead-dark">
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>密码</th>
        <th>邮箱</th>
        <th>BIO</th>
        <th>爱好</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${authors}" var="author" >
        <tr>
            <td>${author.id}</td>
            <td>${author.username}</td>
            <td>${author.password}</td>
            <td>${author.email}</td>
            <td>${author.bio}</td>
            <td>${author.favouriteSection}</td>
        </tr>
    </c:forEach>
    </tbody>
</table>
<nav aria-label="Page navigation example">
    <ul class="pagination justify-content-center">
        <li class="page-item disabled">
            <a class="page-link">Previous</a>
        </li>
        <li class="page-item"><a class="page-link" href="#">1</a></li>
        <li class="page-item"><a class="page-link" href="#">2</a></li>
        <li class="page-item"><a class="page-link" href="#">3</a></li>
        <li class="page-item">
            <a class="page-link" href="#">Next</a>
        </li>
    </ul>
</nav>

<script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js" ></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.bundle.min.js"  ></script>
</body>
</html>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>author list</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/bootstrap.css">
</head>
<body>
<table class="table table-bordered table-hover">
    <thead class="thead-dark">
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>密码</th>
        <th>邮箱</th>
        <th>BIO</th>
        <th>爱好</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${page.results}" var="author" >
        <tr>
            <td>${author.id}</td>
            <td>${author.username}</td>
            <td>${author.password}</td>
            <td>${author.email}</td>
            <td>${author.bio}</td>
            <td>${author.favouriteSection}</td>
        </tr>
    </c:forEach>
    </tbody>
</table>
<nav aria-label="Page navigation example">
    <ul class="pagination justify-content-center">

        <li class="page-item ">
            <a class="page-link" href="${pageContext.request.contextPath}/author/pageList?pageNo=${page.pageNo-1}&pageNo&pageSize=${page.pageSize}">上一页</a>
        </li>
        <!--动态生成页码链接-->
        <li class="page-item">
            <a class="page-link" href="${pageContext.request.contextPath}/author/pageList?pageNo=${page.pageNo+1}&pageNo&pageSize=${page.pageSize}">下一页</a>
        </li>

        <li class="page-item">
            <a class="page-link" href="#"> 当前页:${page.pageNo}/总页码:  <fmt:parseNumber integerOnly="true" value="${page.total/page.pageSize + (page.total/page.pageSize % 1 == 0 ? 0 : 0.5)}" pattern="#" /> </a>
        </li>
    </ul>
</nav>

<script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js" ></script>
<script src="${pageContext.request.contextPath}/static/js/bootstrap.bundle.min.js"  ></script>
</body>
</html>

Dao


import com.lsnu.cross.domain.Author;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class AuthorDaoImpl extends SqlSessionDaoSupport implements AuthorDao {
    @Override
    public List<Author> selectAuthor() {
        return this.getSqlSession().selectList("co m.lsnu.cross.mapper.AuthorMapper.selectAuthors");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值