总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
private String name;
private Integer age;
private String classes;
private String birth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes == null ? null : classes.trim();
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth == null ? null : birth.trim();
}
}
com.generator
Generator.java
package com.generator;
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 = “/generator/generatorConfig.xml”;
String generatorFile = “/generatorConfig.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();
}
}
com.service
StudentService.java
package com.service;
import java.util.List;
import com.entity.Student;
public interface StudentService {
//查询
List selectAll(String keyword);
//删除
int delStudent(int id);
//添加
int insertStudent(Student student);
}
com.service.imp
StudentServiceImpl.java
package com.serviceImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.StudentMapper;
import com.entity.Student;
import com.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Resource
StudentMapper mapper;
@Override
// 查询
public List selectAll(String keyword) {
if (keyword == null || keyword.trim().equals(“”)) {
List selectAll = mapper.selectAll();
return selectAll;
} else {
List likeSelect = mapper.likeSelect(keyword);
return likeSelect;
}
}
// 删除
@Override
public int delStudent(int id) {
int del = mapper.deleteByPrimaryKey(id);
return del;
}
// 添加
@Override
public int insertStudent(Student student) {
int insert = mapper.insert(student);
return insert;
}
}
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:p=“http://www.springframework.org/schema/p”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<context:property-placeholder location=“classpath:jdbc.properties”></context:property-placeholder>
<tx:annotation-driven transaction-manager=“transactionManager” />
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” />
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” />
<mvc:annotation-driven />
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?><context id=“MySQLContext” targetRuntime=“MyBatis3Simple”
defaultModelType=“flat”>
<jdbcConnection driverClass=“com.mysql.jdbc.Driver”
connectionURL=“jdbc:mysql://localhost:3306/stu_db” userId=“root”
password=“root”>
<javaModelGenerator targetPackage=“com.entity”
targetProject=“src”>
<sqlMapGenerator targetPackage=“com.dao”
targetProject=“src” />
<javaClientGenerator targetPackage=“com.dao” type=“XMLMAPPER”
targetProject=“src” />
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/stu_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
WebContent
web.xml
<?xml version="1.0" encoding="UTF-8"?>library.two
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
CharacterEncodingFilter
/*
JSP
Home.jsp
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+“😕/”+request.getServerName()+“:”
+request.getServerPort()+path;
%>
addStudent.jsp
<?xml version="1.0" encoding="UTF-8" ?><%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
最后
由于篇幅原因,就不多做展示了
i=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
最后
[外链图片转存中…(img-fdAga94P-1715429443430)]
[外链图片转存中…(img-QMXakGdQ-1715429443430)]
[外链图片转存中…(img-UgZUqoAL-1715429443430)]
[外链图片转存中…(img-qee9E7jm-1715429443431)]
[外链图片转存中…(img-HCcSgLid-1715429443431)]
[外链图片转存中…(img-ggnmdws0-1715429443431)]
由于篇幅原因,就不多做展示了