org.springframework
spring-webmvc
4.3.7.RELEASE
org.springframework
spring-jdbc
4.3.7.RELEASE
org.springframework
spring-aspects
4.3.7.RELEASE
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
com.mchange
c3p0
0.9.2
mysql
mysql-connector-java
8.0.27
jstl
jstl
1.2
为了减少不必要的麻烦,版本于教程一致。servlet-api和junit的依赖在idea创建时自动引入
引入Bootstrap前端框架下载:https://v3.bootcss.com/
解压后放入static目录下
根据网站的提示进行引入,下载页下方有基本模板
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
JSP - Hello World
创建数据库及表结构
create DATABASE ssm_crud
create table tbl_emp(
emp_id int(11) primary key auto_increment,
emp_name varchar(255) not null,
gender char(1),
email varchar(255),
d_id int(11),
foreign key(d_id) references tbl_dept(dept_id)
)
create table tbl_dept(
dept_id int(11) primary key auto_increment,
dept_name varchar(255) not null
)
编写ssm整合的关键配置文件
web.xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceRequestEncoding
true
forceResponseEncoding
true
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
1
dispatcherServlet
/
hiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
hiddenHttpMethodFilter
/*
配置SpringMVC
dispatcherServlet-servlet.xml
配置Spring
applicationContext.xml
配置MyBatis
使用MyBatis的逆向工程生成对应的bean和mapper
mybatis generator插件地址
GitHub - mybatis/generator: A code generator for MyBatis.https://github.com/mybatis/generator
引入依赖
org.mybatis.generator
mybatis-generator-core
1.3.5
创建逆向工程的配置文件
文件名为mbg.xml
运行如下单元测试方法
@Test
public void test() throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
List warnings = new ArrayList();
boolean overwrite = true;
File configFile = new File("src/main/resources/mbg.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
生成成功,目录结构如下
修改生成的文件为EmployeeMapper接口新增两个方法
在Employee实体类中新增属性
并提供get,set方法
两实体类新增有参,无参构造器
在EmployeeMapper.xml中添加新增方法的实现
mybatis-config.xml
Spring的单元测试
可以自动注入我们需要的组件
引入依赖
org.springframework
spring-test
4.3.7.RELEASE
用法
@RunWith(SpringJUnit4ClassRunner.class)
//指定Spring配置文件的位置
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class WhateverTest {
@Autowired
DepartmentMapper departmentMapper;
@Autowired
EmployeeMapper employeeMapper;
@Test
public void test() {
//插入几个部门
departmentMapper.insertSelective(new Department(null, "删库部"));
departmentMapper.insertSelective(new Department(null, "跑路部"));
//生成员工数据
employeeMapper.insertSelective(new Employee(null, "GYQ", "男", "123456@qq.com", 1));
employeeMapper.insertSelective(new Employee(null, "GYY", "男", "654321@qq.com", 2));
}
}
原来的方式
批量插入员工数据在applicationContext.xml文件中配置一个可执行批量的sqlSession
测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class WhateverTest {
@Autowired
SqlSession sqlSession;
@Test
public void test() {
//批量插入多个员工,使用可以执行批量 *** 作的sqlSession
EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
for (int i = 0; i < 1000; i++) {
String name = UUID.randomUUID().toString().substring(0, 5) + "" + i;
mapper.insertSelective(new Employee(null,name,"男",name+"@test.com",(int)(Math.random()*2+1)));
}
System.out.println("批量添加完成");
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)