环境配置见我的上一篇文章:环境配置
环境搭建好了,现在我们开始建一个类了。这个根据类就是你数据库所建的表所建立,放在src下面的包中,包名自己随意就好,但是配置时记得包名要跟自己写的一样。类中的字段要跟数据库的一样。
目录
pojo类
Book
User
Dao接口
BookDao
UserDao
Service接口:
BookService
UserService
ServiceImpl实现类
BookServiceImpl
UserServiceImpl
Controller层
BookController
UserControler
package com.qiqi.pojo;
public class Book {
private Integer id;
private String bid;
private String name;
private String type;
private double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBid() {
return bid;
}
public void setBid(String bid) {
this.bid = bid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package com.qiqi.pojo;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
下面是Dao接口,是数据持久层,也是底层,负责与数据库进行联络。这里我们不用写Dao的实现类,spring会自动帮我们生成,我们只需要在模块中调用此接口来进行数据业务的处理,而不用关心此接口的具体实现类是哪个类。Dao层的数据源配置,以及有关数据库连接的参数都在Spring的配置文件中进行配置。我这里用的是注解SQL语句,当然也可以用Mpper.xml,全看各位喜好了
package com.qiqi.dao;
import com.qiqi.pojo.Book;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface BookDao {
@Select("select * from t_book")
List findAllBook();
@Select("select * from t_book where bid=#{bid}")
Book findOne(String sid);
@Select("select * from t_book where name like concat(concat('%', #{name}),'%');")
List findName(String name);
@Insert("insert into t_book(bid,name,type,price) values (#{bid},#{name},#{type},#{price})")
int insertBook(Book book);
@Update("update t_book set bid=#{bid},name=#{name},type=#{type},price=#{price} where bid=#{bid}")
int updateBook(Book book);
@Delete("delete from t_book where id=#{id}")
int deleteBook( Integer id);
}
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDao {
@Insert("insert into t_user(name,password) values(#{name},#{password})")
int insert(User user);
@Select("select * from t_user where name=#{name} and password=#{password}")
User findUser(@Param("name") String name ,@Param("password") String password);
}
下面是Service层也就业务逻辑层。这个层是用接口类和实现类两个去执行业务。总之它处于中间位置,它调用DAO层的接口,又要提供接口给Controller层的类来进行调用。他也要进行配置,在spring-config.xml中加入
这个我在上一篇文章中写过。
import com.qiqi.pojo.Book;
import java.util.List;
public interface BookService {
List findAllBook();
Book findOneBook(String bid);
List findName(String name);
int addBook(Book book);
int UpdateBook(Book book);
int deleteBook(Integer id);
}
这边主要是实现图书馆的登录和注册功能
package com.qiqi.service;
import com.qiqi.pojo.User;
import java.util.List;
public interface UserService {
User login(String name,String password);
int addUser(User user);//注册
}
这里用注解@Autowirde实现Bean的自动装配。这个注解是属于 Spring 的容器配置的一个注解,它主要通过Spring 从 ApplicationContext 中先类型再名字的顺序搜寻符合指定类型的所有 bean。类似于“Book book=applicatContext.getBean("student",Student.class)”或bookService = ioc.getBean(BookService.class)等·。具体可看IOC和DI。
package com.qiqi.service.impl;
import com.qiqi.dao.BookDao;
import com.qiqi.pojo.Book;
import com.qiqi.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public List findAllBook() {
return bookDao.findAllBook();
}
@Override
public Book findOneBook(String bid) {
return bookDao.findOne(bid);
}
@Override
public List findName(String name) {
return bookDao.findName(name);
}
@Override
public int addBook(Book book) {
return bookDao.insertBook(book);
}
@Override
public int UpdateBook(Book book) {
return bookDao.updateBook(book);
}
@Override
public int deleteBook(Integer id) {
return bookDao.deleteBook(id);
}
}
package com.qiqi.service.impl;
import com.qiqi.dao.UserDao;
import com.qiqi.pojo.User;
import com.qiqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;//我这里没写private,有需要可以加上去
@Override
public User login(String name,String password) {
return userDao.findUser(name, password);
}
@Override
public int addUser(User user) {
return userDao.insert(user);
}
}
package com.qiqi.controller;
import com.qiqi.pojo.Book;
import com.qiqi.service.BookService;
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.ResponseBody;
import java.util.List;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("findAll")
public String findAllBook( Model model){
model.addAttribute("bookList",bookService.findAllBook());
return "Book";
}
@RequestMapping("addBook")
@ResponseBody
public String addBook(Book book ){
int row= bookService.addBook(book);
if(row>0)
return "ok";
else
return "error";
}
@RequestMapping("findOneBook")
@ResponseBody
public Book findOneBook(String bid){
return bookService.findOneBook(bid);
}
@RequestMapping("findName")
public String findName(String name,Model model){
if (name!=null&&!name.equals("")){
List list=bookService.findName(name);
model.addAttribute("bookList",list);
return "Book";
}
model.addAttribute("error","未查询到相关数据,请输入已有内容");
return "Book";
}
@RequestMapping("updateBook")
@ResponseBody
public String updateBook(Book book){
int row= bookService.UpdateBook(book);
if(row>0)
return "ok";
else
return "error";
}
@RequestMapping("deleteBook")
@ResponseBody
public String deleteBook(Integer id){
int row=bookService.deleteBook(id);
if(row>0)
return "ok";
else
return "error";
}
}
package com.qiqi.controller;
import com.qiqi.pojo.User;
import com.qiqi.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class UserController {
@Resource
private UserService service;
@RequestMapping("userLogin")
public String login(String name,String password,Model model){
User user=service.login(name, password);
if (user!=null){
return "Book";
}
model.addAttribute("logMsg","用户名密码错误或不存在");
return "register";
}
@RequestMapping("registerUser")
@ResponseBody
public String addUser(User user ){
int row= service.addUser(user);
if(row>0)
return "ok";
else
return "error";
}
}
最后一个是视图层,下个文章再写,主要用Bootstrap和CSS技术做美观,利用JQuery提交数据,当然如果感兴趣也可以JSON和Ajax。
下一篇:视图层