看完慕课上的springboot教程 瞬间觉得配置的好少,又双叒叕想到mybatis的简单配置,逆向工程,就想到把两者整合一下,那岂不是完美!说做就做。
com.example.demo;本类是Test类下的逆向工程生成控制类
public class MBGTest {
//本函数是利用mybatis generator自动生成bean下的表 dao下的实现类 mapper下的配置文件
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
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);
System.out.println("成功");
}
}
这是mbg.xml配置文件,逆向工程就是依据本文件来生成mapper
<?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">
<!-- 官方网站:http://www.mybatis.org/generator/configreference/commentGenerator.html -->
<!-- 设置不生成注释 true是不生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/framework"
userId="root"
password="root">
</jdbcConnection>
<!-- -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定javabean生成的位置 -->
<javaModelGenerator
targetPackage="com.example.demo.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>
<!-- 指定到接口生成的位置,mapper接口 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.demo.dao"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- table指定每个表的生成策略 -->
<table tableName="t_admin" domainObjectName="Manager"></table>
<!--
<table tableName="t_dept" domainObjectName="Department"></table> -->
<!-- <table tableName="dept" domainObjectName="Department"></table> -->
<!-- <table tableName="t_category" domainObjectName="Category"></table>
<table tableName="t_information" domainObjectName="Information"></table> -->
<!-- <table tableName="t_comment" domainObjectName="Comment"></table> -->
</context>
</generatorConfiguration>
运行主函数,生成以下的bean dao map(一共四个文件 )
bean:Manager ManagerExample
Manager 实体类,与数据库的表对应
package com.example.demo.bean;
import java.util.Date;
public class Manager {
private Integer id;
private String status;
private Date created;
private String user;
private String password;
private String email;
@Override
public String toString() {
return "Manager{" +
"id=" + id +
", status='" + status + '\'' +
", created=" + created +
", user='" + user + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status == null ? null : status.trim();
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user == null ? null : user.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
}
ManagerExample 用来实现复杂点的sql语句,具体还没用过,我就不贴了
ManagerMapper.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.ManagerMapper">
<resultMap id="BaseResultMap" type="com.example.demo.bean.Manager">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="status" jdbcType="VARCHAR" property="status" />
<result column="created" jdbcType="TIMESTAMP" property="created" />
<result column="user" jdbcType="VARCHAR" property="user" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="email" jdbcType="VARCHAR" property="email" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
id, status, created, user, password, email
</sql>
<select id="selectByExample" parameterType="com.example.demo.bean.ManagerExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from t_admin
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_admin
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_admin
where id = #{id,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.example.demo.bean.ManagerExample">
delete from t_admin
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.example.demo.bean.Manager">
insert into t_admin (id, status, created,
user, password, email
)
values (#{id,jdbcType=INTEGER}, #{status,jdbcType=VARCHAR}, #{created,jdbcType=TIMESTAMP},
#{user,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.example.demo.bean.Manager">
insert into t_admin
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="status != null">
status,
</if>
<if test="created != null">
created,
</if>
<if test="user != null">
user,
</if>
<if test="password != null">
password,
</if>
<if test="email != null">
email,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="status != null">
#{status,jdbcType=VARCHAR},
</if>
<if test="created != null">
#{created,jdbcType=TIMESTAMP},
</if>
<if test="user != null">
#{user,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.example.demo.bean.ManagerExample" resultType="java.lang.Long">
select count(*) from t_admin
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update t_admin
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.status != null">
status = #{record.status,jdbcType=VARCHAR},
</if>
<if test="record.created != null">
created = #{record.created,jdbcType=TIMESTAMP},
</if>
<if test="record.user != null">
user = #{record.user,jdbcType=VARCHAR},
</if>
<if test="record.password != null">
password = #{record.password,jdbcType=VARCHAR},
</if>
<if test="record.email != null">
email = #{record.email,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update t_admin
set id = #{record.id,jdbcType=INTEGER},
status = #{record.status,jdbcType=VARCHAR},
created = #{record.created,jdbcType=TIMESTAMP},
user = #{record.user,jdbcType=VARCHAR},
password = #{record.password,jdbcType=VARCHAR},
email = #{record.email,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.bean.Manager">
update t_admin
<set>
<if test="status != null">
status = #{status,jdbcType=VARCHAR},
</if>
<if test="created != null">
created = #{created,jdbcType=TIMESTAMP},
</if>
<if test="user != null">
user = #{user,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.bean.Manager">
update t_admin
set status = #{status,jdbcType=VARCHAR},
created = #{created,jdbcType=TIMESTAMP},
user = #{user,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
好啦!自动生成的贴完了,只剩下调用测试了!
MAnagerService
/**
* Created by 一伞烟雨 on 2017/8/7.
*/
@Service
public class ManagerService {
@Autowired
ManagerMapper managerMapper;
public List<Manager> getList(){
return managerMapper.selectByExample(null);
}
}
ManagerController
在这里用到了pageHelp
@Controller
public class ManagerController extends BaseAppController{
@Autowired
ManagerService managerService;
@RequestMapping("/get")
@ResponseBody
public String getAllList(@RequestParam(value="pn",defaultValue="1")Integer pn, Model model){
PageHelper.startPage(pn,5);
List<Manager> mm = managerService.getList();
PageInfo page = new PageInfo(mm,5);
data.put("PageInfo",page);
System.out.println("Manger:"+mm.toString());
return successData();
}
}
spring boot启动类 注意上面的扫描注解
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class AiaxExampleApplication {
public static void main(String[] args) {
SpringApplication.run(AiaxExampleApplication.class, args);
}
}
当然还有最重要的yml配置文件啊
server:
port: 8080
# context-path: /ajax
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/framework?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: com.example.demo.dao
# configLocation: classpath:/mybatis-config.xml
好了!到此为止,完美跑起来。太棒了!