ssm整合,使用mybatis的逆向工程实现代码。
mybatis的逆向工程:mgb.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>
<!--targetRuntime 可以设置生成的代码功能级别
MyBatis3Simple 标配版 CRUD
MyBatis3 豪华版-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 去掉全部的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--修改数据库的连接属性-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm"
userId="root"
password="123123">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--生成JavaBean的
targetPackage="com.atguigu.pojo" 生成的JavaBean的包名
targetProject=".\mbg\src" 生成的java源代码存放的位置-->
<javaModelGenerator targetPackage="com.atguigu.pojo" targetProject=".\day12ssm\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--
生成mapper.xml配置文件-->
<sqlMapGenerator targetPackage="com.atguigu.mapper" targetProject=".\day12ssm\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mapper" targetProject=".\day12ssm\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 一个table标签,表示数据库中的一张表
tableName 数据库表名
domainObjectName 生成的类名-->
<table tableName="t_book" domainObjectName="Book" />
</context>
</generatorConfiguration>
mybatis逆向工程启动类:MBGRunner
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MBGRunner {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//需要填写配置文件名
File configFile = new File("day12ssm/mgb.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);
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--包扫描 除了web组件,我都要。-->
<context:component-scan base-package="com.atguigu">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--jdbc-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--连接池-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
</bean>
<!-- Mybatis整合Spring的核心配置之一 -->
<!--把mybatis配置到spring容器中,需要配置 sqlSessionFactory,需要 mybatis-config.xml -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!--配置 sqlSessionFactory 需要的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--配置访问数据库需要的连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--Mybatis整合Spring的核心配置之二-->
<!--配置mapper接口扫描,放到spring容器中-->
<mybatis-spring:scan base-package="com.atguigu.mapper"/>
<!--添加事务-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <!–事务的代理 使用标签配置事务–>
<tx:annotation-driven transaction-manager="transactionManager"/>
-->
<!--标配-->
<!--静态资源处理配置-->
<mvc:default-servlet-handler/>
<!--使用注解配置-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置事务属性 -->
<tx:advice id="tx_ssm" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config>
<aop:advisor advice-ref="tx_ssm"
pointcut="execution(* com..service..*.*(..))" />
</aop:config>
</beans>
jdbc.properties:
user=root
password=123123
url=jdbc:mysql://localhost:3306/ssm
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
log4j.properties:
# Global logging configuration
# 全局日志配置
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
# log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 将积极加载改为消极加载 按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!--需要导入sql配置文件-->
<mappers>
<!--在 dao 包下匹配全部sql语句-->
<package name="com.atguigu.mapper"/>
</mappers>
</configuration>
整体架构:
BookController:
@RequestMapping(value = "/book")
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/list")
public String queryAll(){
bookService.queryAll().forEach(System.out::println);
return "redirect:/index.jsp";
}
}
BookService:
public interface BookService {
public void addBook(Book book);
public void updateBook(Book book);
public void deleteBookById(Integer id);
public Book queryBookById(Integer id);
public List<Book> queryAll();
}
BookServicelmpl:
@Service
public class BookServicelmpl implements BookService {
@Autowired
private BookMapper bookMapper;
@Override
public void updateBook(Book book) {
bookMapper.updateByPrimaryKeySelective(book);
}
@Override
public void deleteBookById(Integer id) {
bookMapper.deleteByPrimaryKey(id);
}
@Override
public Book queryBookById(Integer id) {
return bookMapper.selectByPrimaryKey(id);
}
@Override
public List<Book> queryAll() {
return bookMapper.selectByExample(null);
}
}
javaBean : Book:
public class Book {
private Integer id;
private String name;
private String author;
private BigDecimal price;
private Integer sales;
private Integer stock;