说明:项目工程采用 maven 管理,maven 工程的建立参见:
https://blog.csdn.net/weixin_38533896/article/details/7976818
Spring + SpringMVC+ Mybatis 相关 .xml 文件配置参见:
https://blog.csdn.net/weixin_38533896/article/details/79863620
本文是接续上两篇博客完成的。
1. 建立数据表
2. 工程中按照功能建立对应的包,在src/main/resources下建立 mapper 文件夹用于存放逆向工程的映射文件
3. 引入mybatis-generater 的 jar 包,此 jar包的引入已经写入 pom.xml 文件
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency>4. 在工程的根目录建立 mbg.xml 文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 屏蔽生成代码的注释 --> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 配置数据库链接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8&useSSL=false" userId="root" password="402664107"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean 的生成位置 --> <javaModelGenerator targetPackage="com.lbc.crud.bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 指定sql 映射文件生成位置 --> <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定dao 接口生成的位置,mapper 接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lbc.crud.dao" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- table 指定每个表的生成策略 --> <table tableName="tbl_emp" domainObjectName="Employee"></table> <table tableName="tbl_dept" domainObjectName="Department"></table> </context> </generatorConfiguration>5. 在 test 包下建立MBGTest.java,运行生成数据表对应的 bean、dao
public class MBGTest { public static void main(String[] args) throws Exception { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File("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); } }
6. 在 test 包下建立 MapperTest.java 测试增删改查。注意:需要导入 Spring 单元测试 jar 包,此 jar 包导入已写入pom.xml
<!-- spring单元测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> <scope>test</scope> </dependency>
MapperTest.java
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) public class MapperTest { @Autowired DepartmentMapper departmentMapper; @Autowired EmployeeMapper employeeMapper; @Autowired SqlSession sqlSession; @Test public void testDeptInsert() { int insertSelective = departmentMapper.insertSelective(new Department(null, "开发部")); System.out.println(insertSelective); } @Test public void testDeptSelect() { Department selectByPrimaryKey = departmentMapper.selectByPrimaryKey(1); System.out.println(selectByPrimaryKey); } @Test public void testEmpSelectByKey() { Employee selectByPrimaryKeyWithDept = employeeMapper .selectByPrimaryKeyWithDept(2); System.out.println(selectByPrimaryKeyWithDept); } @Test public void testEmpSelectByExample() { EmployeeExample employeeExample = new EmployeeExample(); Criteria criteria = employeeExample.createCriteria(); criteria.andEmpIdLessThan(5); List<Employee> listTeammembers =employeeMapper.selectByExampleWithDept(employeeExample); for(Employee e : listTeammembers){ System.out.println(e); } } @Test //测试批量插入 public void testEmpInsert() { EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); for (int i = 0; i < 1000; i++) { String uid = UUID.randomUUID().toString().substring(0, 5) + i; mapper.insertSelective(new Employee(null, uid, "M", uid + "@atguigu.com", 1)); } }7. 测试结果
至此,SSM 框架已经实现 控制台与数据库的接通,接下来进行控制台与前端界面的联合调试。