1. 程式人生 > >Java後臺SSM框架的簡單使用

Java後臺SSM框架的簡單使用

使用步驟:

Controller類  ---> Service介面(serviceImpl實現Service介面類)-->DAO介面(DaoImpl實現DAO介面類)-->Mapper介面(通過Mybatis實現介面方法)。

Controller類:

package com.cuboo.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cuboo.entity.User;
import com.cuboo.service.JsonService;

/**
 * 
 * @author cuboo
 * 返回Json資料
 *
 */

@Controller
public class JsonController {
	
	@Resource
	JsonService service;
	
	@ResponseBody
	@RequestMapping("/getJson")
	public User getUserInfo(){
		User user = new User();
		user.setPassword("123456789");
		user.setUsername("jsondataset");
		return user;
	}
	
	
	@RequestMapping("/getUserJson")
	@ResponseBody
	public User getUser(@RequestParam String getusername){
		User user = new User();
		try {
			user = service.getInfoByName(getusername);
		} catch (Exception e) {
		   e.printStackTrace();
		}
		return user;
	}
    
	@RequestMapping("/getAllUserJson")
	@ResponseBody
	public List<User> getAllUser() {
	
		return service.getAllUser();
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public String addUsers(@RequestParam int id,@RequestParam String getusername
			,@RequestParam String getpassword){
		service.addoneUser(id,getusername,getpassword);
		return "新增成功";
	}
	
	@RequestMapping("/deleteUser")
	@ResponseBody
	public String delUserById(int id) {
		service.delUserById(id);
		return "刪除成功";
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateById(@RequestParam int id,@RequestParam String getpassword) {
		service.updateUserById(id,getpassword);
		return "更新完成";
	}
}

Service介面(serviceImpl實現Service介面類):
package com.cuboo.service;

import java.util.List;

import com.cuboo.entity.User;

public interface JsonService {
	
	public User getInfoByName(String username) throws Exception;
	public List<User> getAllUser();
	public void addoneUser(int id,String getusername,String getpassword);
	public void delUserById(int id); 
	public void updateUserById(int id,String password); 
}
package com.cuboo.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.cuboo.dao.UserDao;
import com.cuboo.entity.User;

@Service
public class JsonServiceImpl implements JsonService{
    @Resource
    UserDao dao;
    
    @Override
	public User getInfoByName(String username) throws Exception {
		User user = dao.getInfoByUserName(username);
		if (user == null) {
			throw new Exception("使用者不存在!");
		}
		return user;
	}
    @Override
	public List<User> getAllUser() {
		return dao.getAllUserinfo();
	}
    @Override
	public void addoneUser(int id,String getusername,String getpassword) {
		User user = new User();
		user.setId(id);
		user.setUsername(getusername);
		user.setPassword(getpassword);
		dao.addUser(user);
	}
    @Override
	public void delUserById(int id) {
		dao.deleteUserById(id);
	}
    @Override
	public void updateUserById(int id,String password) {
		User user = new User();
		user.setId(id);
		user.setPassword(password);
		dao.updateUserById(user);
	}
}
DAO介面(DaoImpl實現DAO介面類):
package com.cuboo.dao;


import java.util.List;

import org.springframework.stereotype.Repository;

import com.cuboo.entity.User;

public interface UserDao {

	public User getInfoByUserName(String username);
	public List<User> getAllUserinfo();
	public void addUser(User user);
	public void deleteUserById(int id);
	public void updateUserById(User user);
	
}

package com.cuboo.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.cuboo.entity.User;
import com.cuboo.mapper.UserMapper;

@Repository
public class UserDaoImpl implements UserDao{
	@Resource
	UserMapper mapper;

	@Override
	public User getInfoByUserName(String username) {
		
		return mapper.getInfoByUserName(username);
	}

	@Override
	public List<User> getAllUserinfo() {
		
		return mapper.getAllUserinfo();
	}

	@Override
	public void addUser(User user) {
		mapper.addUser(user);
		
	}

	@Override
	public void deleteUserById(int id) {
		
		mapper.deleteUserById(id);
	}

	@Override
	public void updateUserById(User user) {
	
		mapper.updateUserById(user);
		
	}

}

Mapper介面(通過Mybatis實現介面方法):
package com.cuboo.mapper;

public interface BaseMapper {

}


package com.cuboo.mapper;

import java.util.List;

import com.cuboo.entity.User;

public interface UserMapper extends BaseMapper{
	
	public User getInfoByUserName(String username);
	public List<User> getAllUserinfo();
	public void addUser(User user);
	public void deleteUserById(int id);
	public void updateUserById(User user);
}


其中用到的實體類:
package com.cuboo.entity;

import java.io.Serializable;

public class User implements Serializable  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8261353151151101737L;
	private Integer id;
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	

}

Mybatis對mapper介面的實現
<?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">
  <!-- 對映DAO/Mapper介面 -->
    <mapper namespace="com.cuboo.mapper.UserMapper" >
     <!-- List的返回型別設定 -->
      <!-- mybatis的config.xim 配置實體類對映 的alias-->
    <resultMap id="UserResultMap" type="User">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
    </resultMap>
    
      <!-- id dao介面的方法名  parameterType、resultType 分別為dao中介面方法的引數何返回型別-->
    <select id="getInfoByUserName" parameterType="String" resultType="User">
        select * from t_user where username = #{username}
    </select>
    
    <select id="getAllUserinfo" resultType="UserResultMap">
        select * from t_user
    </select>
    
   <insert id="addUser" parameterType="User">
        insert into t_user(id,username,password)
        values (#{id},#{username},#{password})
    </insert> 
    
    <delete id="deleteUserById" parameterType="int" >
        delete from  t_user where id = #{id}
    </delete>
     <update id="updateUserById" parameterType="User">
        update t_user set password = #{password}
        where id = #{id}
    </update>
    
   <!--  <update id="updateUserById" parameterType="User">
        update t_user set username = #{username},password = #{password}
        where id = #{id}
    </update> -->
</mapper>
具體的相關程式碼和配置檔案及jar包詳見下載連結: