接上回,我们继续搭建项目,争取多集成一些功能
集成mybatis
引入数据库连接池、mysql驱动,mybatis,pagehelper分页,generator逆向生成代码等依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!--mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<!--pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
- generator逆向生成代码
先在根目录下新建generatorConfig.xml文件,文件内容如下
<generatorConfiguration>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.zwq.utils.MyMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/hrm_db"
userId="root"
password="123456">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.zwq.entity" targetProject="src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.zwq.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="dept_inf"></table>
<table tableName="document_inf"></table>
<table tableName="employee_inf"></table>
<table tableName="job_inf"></table>
<table tableName="menu_inf"></table>
<table tableName="notice_inf"></table>
<table tableName="user_inf"></table>
</context>
</generatorConfiguration>
在java空间新建utils包,自定义MyMapper类继承 Mapper<T>, MySqlMapper<T>,再自定义逆向工程启动类GeneratorDisplay
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.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);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
通过运行main方法就可以生成我们所需要的mapper,entity等文件了,
由于使用了公共的Mapper接口,我们可以像JPA一样使用一些默认方法操作增删改查。
mybatis配置
先配置数据源,然后在application.properties中增加
mybatis.type-aliases-package=com.zwq.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.zwq.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params.count=countSql
启动类中添加mybatis的mapper包扫描@MapperScan(basePackages = "com.zwq.mapper")
Service的创建
新建一个HrmService接口,我们先写一些和UserInf相关的内容,方法如下
/**
* 用户登录
* @param loginname
* @param password
* @return
*/
UserInf login(String loginname, String password);
/**
* 根据id 查询用户
* @param id
* @return
*/
UserInf findUserById(Integer id);
/**
* 获得所有用户(分页)
* @param user
* @param page
* @param pageSize
* @return
*/
List<UserInf> findUser(UserInf user,Integer page, Integer pageSize);
/**
* 根据id删除用户
* @param id
*/
void removeUserById(Integer id);
/**
* 修改用户
* @param user
*/
void modifyUser(UserInf user);
/**
* 添加用户
* @param user
*/
void addUser(UserInf user);
然后依次写实现,由于使用了公共Mapper,单表的操作及其的简单
我们可以直接用userInfMapper去调用相关方法,值得注意的是,分页接口,只需要一开始调用PageHelper.startPage(page, pageSize);即可实现分页功能,十分方便,分页代码如下
@Override
public List<UserInf> findUser(UserInf user, Integer page, Integer pageSize) {
PageHelper.startPage(page, pageSize);
Example example = new Example(UserInf.class);
Example.Criteria criteria = example.createCriteria();
if (!StringUtils.isEmptyOrWhitespace(user.getUsername())) {
criteria.andLike("username", "%" + user.getUsername() + "%");
}
return userInfMapper.selectByExample(example);
}
测试结果
测试结果
代码已经上传到了github 点击跳转
网友评论