MovieController
package com.ninemax.movie.controller; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.ninemax.movie.model.Movie; import com.ninemax.movie.service.MovieService; /** * Controller * * @author Darker * */ @Controller @RequestMapping("/movie") public class MovieController { @Resource private MovieService movieService; /** * 查询电影信息 * * @param req * @param resp * @return */ @RequestMapping("/movielist") public String findByAllMovie(HttpServletRequest req, HttpServletResponse resp) { List<Movie> list = movieService.findAllMovie(); int sumTotal = list.size(); req.setAttribute("movielist", list); req.setAttribute("sumTotal", sumTotal); return "movie/MovieList"; } /** * 跳转到添加电影信息页面 * * @return */ @RequestMapping("/skipAdd") public String skipAdd() { return "movie/skipAdd"; } /** * 添加电影信息 * * @return */ @RequestMapping("/addMovie") public String addMovie(String movieName, String movieTime, String movieMoney, String movieNumber, String flag) { Movie movie = new Movie(null, movieName, movieTime, movieMoney, movieNumber, flag); movieService.addMovie(movie); return "redirect:/movie/movielist"; } /** * 跳转到更新页面 */ @RequestMapping("/toUpdateMovie") public String toUpdateMovie(HttpServletRequest req, Integer id) { Movie movie = movieService.findMovieById(id); req.setAttribute("movies", movie); return "movie/updateMovie"; } /** * 更新电影信息 * * @return */ @RequestMapping("/updateMovie") public String updateMovie(int movieId, String movieName, String movieTime, String movieMoney, String movieNumber, String flag) { Movie movie = new Movie(movieId, movieName, movieTime, movieMoney, movieNumber, flag); movieService.updateMovie(movie); return "redirect:/movie/movielist"; } /** * 删除 * * @param id * @return */ @RequestMapping("/deleteMovie") public void deleteMovie(HttpServletResponse rep, int id) { try { movieService.updateFalseMovie(id); // 设置ajax返回类型 rep.setContentType("text/html;charset=UTF-8"); // 得到输出流 OutputStream printWriter; printWriter = rep.getOutputStream(); // 输出提示信息 printWriter.write("success".getBytes()); //关闭链接 printWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ } } }
MovieService
package com.ninemax.movie.service; import java.util.List; import org.springframework.stereotype.Service; import com.ninemax.movie.model.Movie; @Service public interface MovieService { //查询 public List<Movie> findAllMovie(); //添加 public void addMovie(Movie movie); //根据ID查找信息 public Movie findMovieById(Integer MovieId); //更新 public void updateMovie(Movie movie); //假删除 public void updateFalseMovie(int id); }
MovieServiceImpl
package com.ninemax.movie.service.impl; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Repository; import com.ibatis.sqlmap.client.SqlMapClient; import com.ninemax.movie.model.Movie; import com.ninemax.movie.service.MovieService; @Repository public class MovieServiceImpl implements MovieService { @Resource private SqlMapClient sqlMapClient; @SuppressWarnings("unchecked") @Override public List<Movie> findAllMovie() { // TODO Auto-generated method stub List<Movie> movies = null; try { movies = sqlMapClient.queryForList("selmovie", -1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return movies; } @Override public void addMovie(Movie movie) { // TODO Auto-generated method stub try { sqlMapClient.insert("addmovie", movie); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public Movie findMovieById(Integer MovieId) { // TODO Auto-generated method stub Movie movies = null; try { movies = (Movie) sqlMapClient.queryForObject("selMovieById", MovieId); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return movies; } @Override public void updateMovie(Movie movie) { // TODO Auto-generated method stub try { sqlMapClient.update("udmovie", movie); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void updateFalseMovie(int id) { // TODO Auto-generated method stub Map<String, Integer> map = new HashMap<String, Integer>(); map.put("movieId", id); map.put("flag", -1); try { sqlMapClient.update("falseDelMovie", map); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Movie
package com.ninemax.movie.model; public class Movie { private Integer movieId; private String movieName; private String movieTime; private String movieMoney; private String movieNumber; private String flag; public Movie(){ } public Movie(Integer movieId, String movieName, String movieTime, String movieMoney, String movieNumber, String flag) { super(); this.movieId = movieId; this.movieName = movieName; this.movieTime = movieTime; this.movieMoney = movieMoney; this.movieNumber = movieNumber; this.flag = flag; } @Override public String toString() { return "Movie [movieId=" + movieId + ", movieName=" + movieName + ", movieTime=" + movieTime + ", movieMoney=" + movieMoney + ", movieNumber=" + movieNumber + "]"; } public Integer getMovieId() { return movieId; } public void setMovieId(Integer movieId) { this.movieId = movieId; } public String getMovieName() { return movieName; } public void setMovieName(String movieName) { this.movieName = movieName; } public String getMovieTime() { return movieTime; } public void setMovieTime(String movieTime) { this.movieTime = movieTime; } public String getMovieMoney() { return movieMoney; } public void setMovieMoney(String movieMoney) { this.movieMoney = movieMoney; } public String getMovieNumber() { return movieNumber; } public void setMovieNumber(String movieNumber) { this.movieNumber = movieNumber; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } }
Movie-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="Movie"> <typeAlias alias="Movie" type="com.ninemax.movie.model.Movie" /> <resultMap id="MovieResult" class="Movie"> <result property="movieId" column="movieId" /> <result property="movieName" column="movieName" /> <result property="movieTime" column="movieTime" /> <result property="movieMoney" column="movieMoney" /> <result property="movieNumber" column="movieNumber" /> </resultMap> <insert id="addmovie" parameterClass="Movie"> insert movie(movieId,movieName,movieTime,movieMoney,movieNumber,flag) values(#movieId#,#movieName#,#movieTime#,#movieMoney#,#movieNumber#,#flag#) </insert> <select id="selmovie" parameterClass="int" resultClass="Movie"> select * from movie where flag!=#flag# </select> <select id="selMovieById" parameterClass="int" resultClass="Movie"> select * from movie where movieId=#movieId# </select> <delete id="delmovie" parameterClass="int"> delete from movie where movieId=#movieId# </delete> <update id="udmovie" parameterClass="Movie"> update movie set movieName=#movieName#, movieTime=#movieTime#, movieMoney=#movieMoney#, movieNumber=#movieNumber# where movieId=#movieId# </update> <!-- 假删除 --> <update id="falseDelMovie" parameterClass="java.util.HashMap" > update movie set flag=#flag# where movieId=#movieId# </update> </sqlMap>
博主的基础群:260052172(菜鸟的天堂,大神莫入)