ssssss

package cn.test.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.test.po.User;
import cn.test.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/queryUserByUid")
    public String queryUser(int uid, Model model) {
        User u = userService.queryUserByUid(uid);
        model.addAttribute("user", u);
        return "updataUser";
    }
    @RequestMapping("updataUser")
    public String updataUser(User user) {
        int t = userService.updataUser(user);
        if (t > 0) {
            return "forward:getAllUser2";
        } else {
            return "erro";
        }
    }

    @RequestMapping("/getAllUser")
    public String getAllUser(Model model, @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> u = userService.getAllUser();

        PageInfo<User> p = new PageInfo<User>(u);
        model.addAttribute("ulist", u);
        model.addAttribute("pageinfo", p);

        return "userinfo";
    }

    

    @RequestMapping("/queryUserByUname")
    public String queryByUname(String uname, Model model) {
        
        List<User> u = userService.queryByUname(uname);
        model.addAttribute("ulist", u);
        
        return "userinfo";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    @RequestMapping("/addUser")
    public String addUser(User user) {
        int t = userService.addUser(user);
        if (t > 0) {
            return "forward:getAllUser";
        } else {
            return "erro";

        }
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int uid) {
        int t = userService.deleteUser(uid);
        if (t > 0) {
            return "forward:getAllUser";
        } else {
            return "erro";

        }
    }

}

---------------------------------------------------------------------------------------------
usermapper

package cn.test.mapper;

import java.util.List;

import cn.test.po.User;

public interface UserMapper {
    public User queryUserByUid(int uid) ;
    public int updataUser(User user);
    
    public List<User> getAllUser() ;
        
    public List<User> queryByUname(String uname);
    public int addUser(User user);
    public int deleteUser(int uid);

------------------------------------------------------------------------

usermapper.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="cn.test.mapper.UserMapper">
     <select id="queryUserByUid" parameterType="Integer"
         resultType="cn.test.po.User">
         select * from userTab where uid=#{_parameter}
     
     </select>
     <update id="updataUser" parameterType="cn.test.po.User">
        update mes set uname=#{uname},uage=#{uage},umark=#{umark} where uid=#{uid}
    </update>
    
 <select id="getAllUser" resultType="cn.test.po.User">
 select * from usertab
 
 </select>
 
 <select id="queryByUnam" parameterType="String" resultType="cn.test.po.User">
 select * from usertab where uname like concat('',#{-parameter},'%')
 
 </select>
 
 <insert id="addUser" parameterType="cn.test.po.User">
     insert into usertab(uname,age) values(#{uname},#{age})
 </insert>
 <delete id="deleteUser" parameterType="Integer">
     delete from usertab where uid=#{_parameter}
 </delete>
 
 </mapper>

----------------------------------------------------------------------------

service

package cn.test.service;

import java.util.List;

import cn.test.po.User;

public interface UserService {

    public User queryUserByUid(int uid) ;
    public int updataUser(User user);
    
    public List<User> getAllUser() ;
    public List<User> queryByUname(String uname) ;
    public int addUser(User user);
    public int deleteUser(int uid);
}

---------------------------------------------------------------------------

impl

package cn.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import cn.test.mapper.UserMapper;
import cn.test.po.User;

public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    
    @Override
    public User queryUserByUid(int uid) {
        // TODO Auto-generated method stub
        return userMapper.queryUserByUid(uid);
        
    }
    
    @Override
    public int updataUser(User user) {
        
        return userMapper.updataUser(user);
    }

    @Override
    public List<User> getAllUser() {
        // TODO Auto-generated method stub
        return userMapper.getAllUser();
    }

    @Override
    public List<User> queryByUname(String uname) {
        // TODO Auto-generated method stub
        return userMapper.queryByUname(uname);
    }

    @Override
    public int addUser(User user) {
        // TODO Auto-generated method stub
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUser(int uid) {
        // TODO Auto-generated method stub
        return userMapper.deleteUser(uid);
    }

}

----------------------------------------------------------------------------------------
applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

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


    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <tx:annotation-driven
        transaction-manager="transactionManager" />


    <bean id="sqlsessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="plugins">
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <props>
                        <prop key="dialect">mysql</prop>
                        <prop key="reasonable">true</prop>
                    </props>
                </property>
            </bean>
        </property>


    </bean>


    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.test.mapper"></property>
    </bean>

    <context:component-scan
        base-package="cn.test.service"></context:component-scan>
</beans>

上一篇:Maven查找jar包的依赖坐标


下一篇:Java 自定义注解结合 Aop 切面和本地缓存实现接口防重复请求提交