基于Spring MVC + Spring + MyBatis的【图书信息管理系统(二)】

(6)创建MyBatis操作数据库所需的Mapper接口及其Xml映射数据库操作语句文件。

(7)创建业务逻辑相应的接口及其实现类,实现相应的业务,并在类中加入对DAO/Mapper的引用和注入。

(8)创建Controller控制器类,在Controller中添加对业务逻辑类的引用和注入,并配置springMVC配置文件。

(9)创建相关的操作页面,并使用CSS对页面进行美化。

(10)实现页面的各项操作功能,操作要人性化。

(11)调试运行成功后导出相关的数据库文件并提交。

五、实现代码

=========================================================================

1、MySQL数据库


person_db.sql

在这里插入图片描述

/*

Date: 2021-07-20 12:46:17

*/

SET FOREIGN_KEY_CHECKS=0;


– Table structure for tb_person


DROP TABLE IF EXISTS tb_person;

CREATE TABLE tb_person (

id int(11) NOT NULL AUTO_INCREMENT,

BookName varchar(50) NOT NULL,

author varchar(10) NOT NULL,

pages int(11) NOT NULL,

createTime datetime NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;


– Records of tb_person


INSERT INTO tb_person VALUES (‘1’, ‘《红楼梦》’, ‘曹雪芹’, ‘542’, ‘2021-07-20 10:44:26’);

INSERT INTO tb_person VALUES (‘2’, ‘《水浒传》’, ‘施耐庵’, ‘652’, ‘2021-07-20 10:44:57’);

INSERT INTO tb_person VALUES (‘3’, ‘《镜花缘》’, ‘李汝珍’, ‘441’, ‘2021-07-20 10:45:59’);

INSERT INTO tb_person VALUES (‘5’, ‘《西游记》’, ‘吴承恩’, ‘5000’, ‘2021-07-20 12:42:43’);

INSERT INTO tb_person VALUES (‘7’, ‘《三国演义》’, ‘罗贯中’, ‘10000’, ‘2021-07-20 12:42:07’);

2、项目Java代码


目录结构

person_db

在这里插入图片描述

JAR包:

在这里插入图片描述在这里插入图片描述

src

com.controller

PersonController.java

package com.controller;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.TbPerson;

import com.service.PersonService;

import com.sun.org.apache.bcel.internal.generic.NEW;

@Controller

public class PersonController {

@Resource

private PersonService service;

@RequestMapping(“selectAll”)

public String selectAll(Model model) {

List list=service.selsctAll();

model.addAttribute(“personList”, list);

return “/person”;

}

@RequestMapping(“deleteByid”)

public String deleteByid(int id) {

int rows=service.delete(id);

return “redirect:/selectAll.do”;

}

//添加页面跳转

@RequestMapping(“getpage”)

public String getpage() {

return “/addPage”;

}

//添加

@RequestMapping(“addPerson”)

public String addPerson(TbPerson tbPerson) {

SimpleDateFormat simple=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

tbPerson.setCreatetime(simple.format(new Date()));

int rows=service.addPerson(tbPerson);

return “redirect:/selectAll.do”;

}

//根据id查询

@RequestMapping(“getbyid”)

public String getbyid(Model model,int id) {

TbPerson tbPerson=service.getByid(id);

model.addAttribute(“tbPerson”, tbPerson);

return “/addPage”;

}

//修改

@RequestMapping(“update”)

public String update(TbPerson tbPerson) {

SimpleDateFormat simple=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

tbPerson.setCreatetime(simple.format(new Date()));

int rows=service.update(tbPerson);

return “redirect:/selectAll.do”;

}

}

com.dao

TbPersonMapper.java

package com.dao;

import com.entity.TbPerson;

import java.util.List;

public interface TbPersonMapper {

int deleteByPrimaryKey(Integer id);

int insert(TbPerson record);

TbPerson selectByPrimaryKey(Integer id);

List selectAll();

int updateByPrimaryKey(TbPerson record);

}

TbPersonMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

delete from tb_person

where id = #{id,jdbcType=INTEGER}

insert into tb_person (id, BookName, author,

pages, createTime)

values (#{id,jdbcType=INTEGER}, #{bookname,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR},

#{pages,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP})

update tb_person

set BookName = #{bookname,jdbcType=VARCHAR},

author = #{author,jdbcType=VARCHAR},

pages = #{pages,jdbcType=INTEGER},

createTime = #{createtime,jdbcType=TIMESTAMP}

where id = #{id,jdbcType=INTEGER}

select id, BookName, author, pages, createTime

from tb_person

where id = #{id,jdbcType=INTEGER}

select id, BookName, author, pages, createTime

from tb_person ORDER BY createTime DESC

com.entity

TbPerson.java

package com.entity;

import java.util.Date;

public class TbPerson {

private Integer id;

private String bookname;

private String author;

private Integer pages;

private String createtime;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getBookname() {

return bookname;

}

public void setBookname(String bookname) {

this.bookname = bookname == null ? null : bookname.trim();

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author == null ? null : author.trim();

}

public Integer getPages() {

return pages;

}

public void setPages(Integer pages) {

this.pages = pages;

}

public String getCreatetime() {

return createtime;

}

public void setCreatetime(String createtime) {

this.createtime = createtime;

}

}

com.service

PersonService.java

package com.service;

import java.util.List;

import com.entity.TbPerson;

public interface PersonService {

//查询所有

List selsctAll();

//删除

int delete(int id);

//根据id查询

TbPerson getByid(int id);

//修改

int update(TbPerson tbPerson);

//添加

int addPerson(TbPerson tbPerson);

}

com.service.impl

PersonServiceImpl.java

package com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.TbPersonMapper;

import com.entity.TbPerson;

import com.service.PersonService;

@Service

public class PersonServiceImpl implements PersonService {

@Resource

private TbPersonMapper mapper;

@Override

public List selsctAll() {

// TODO Auto-generated method stub

List list=mapper.selectAll();

return list;

}

@Override

public int delete(int id) {

// TODO Auto-generated method stub

int rows=mapper.deleteByPrimaryKey(id);

return rows;

}

@Override

public TbPerson getByid(int id) {

// TODO Auto-generated method stub

TbPerson tbPerson=mapper.sel
ectByPrimaryKey(id);

return tbPerson;

}

@Override

public int update(TbPerson tbPerson) {

// TODO Auto-generated method stub

int rows=mapper.updateByPrimaryKey(tbPerson);

return rows;

}

@Override

public int addPerson(TbPerson tbPerson) {

// TODO Auto-generated method stub

int rows=mapper.insert(tbPerson);

return rows;

}

}

genter

Generator.java

package genter;

import java.io.IOException;

import java.io.InputStream;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;

import org.mybatis.generator.config.Configuration;

import org.mybatis.generator.config.xml.ConfigurationParser;

import org.mybatis.generator.exception.InvalidConfigurationException;

import org.mybatis.generator.exception.XMLParserException;

import org.mybatis.generator.internal.DefaultShellCallback;

public class Generator {

/*

  • targetRuntime=“MyBatis3Simple”, 不生成Example

*/

public void generateMyBatis() {

//MBG执行过程中的警告信息

List warnings = new ArrayList();

//当生成的代码重复时,覆盖原代码

boolean overwrite = true ;

String generatorFile = “/generatorConfig.xml”;

//String generatorFile = “/generator/generatorConfigExample.xml”;

//读取MBG配置文件

InputStream is = Generator.class.getResourceAsStream(generatorFile);

ConfigurationParser cp = new ConfigurationParser(warnings);

Configuration config;

try {

config = cp.parseConfiguration(is);

DefaultShellCallback callback = new DefaultShellCallback(overwrite);

//创建MBG

MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

//执行生成代码

myBatisGenerator.generate(null);

} catch (IOException e) {

e.printStackTrace();

} catch (XMLParserException e) {

e.printStackTrace();

} catch (InvalidConfigurationException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

for (String warning : warnings) {

System.out.println(warning);

}

}

public static void main(String[] args) {

Generator generator = new Generator();

generator.generateMyBatis();

}

}

MyBatis

SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
spring

applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">

<context:property-placeholder location=“classpath:dataSource.properties”/>

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">

<context:component-scan base-package=“com.service”></context:component-scan>

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

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">

<context:component-scan base-package=“com.controller”></context:component-scan>

mvc:annotation-driven</mvc:annotation-driven>

dataSource.properties

db.driverClass=com.mysql.jdbc.Driver

db.jdbcUrl=jdbc:mysql://127.0.0.1:3306/person_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false

db.user=root

db.password=123456

generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>

<jdbcConnection driverClass=“com.mysql.jdbc.Driver”

connectionURL=“jdbc:mysql://localhost:3306/person_db”

userId=“root” password=“123456”>

<javaClientGenerator targetPackage=“com.dao” type=“XMLMAPPER”

targetProject=“src”/>

WebContent

web.xml

<?xml version="1.0" encoding="UTF-8"?>

person_db

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

contextConfigLocation

classpath:spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-mvc.xml

1

springmvc

*.do

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

JSP

index.jsp

<%@ page language=“java” contentType=“text/html; charset=utf-8”

pageEncoding=“utf-8”%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值