第一步创建maven项目  配置pom.

<?  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\">
		<!-- 配置数据连接信息 -->
		<!-- 	去掉注释suppressDate是去掉生成日期那行注释suppressAllComments是去掉所有的注解 -->
		<commentGenerator>
			<property name=\"suppressDate\" value=\"true\" />
			<property name=\"suppressAllComments\" value=\"true\" />
		</commentGenerator>
		<jdbcConnection driverClass=\"com.mysql.jdbc.Driver\"
			connectionURL=\"jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8\" userId=\"root\"
			password=\"123456\">
		</jdbcConnection>

		<javaTypeResolver>
			<property name=\"forceBigDecimals\" value=\"false\" />
		</javaTypeResolver>

		<!-- 指定java bean生成的位置 -->
		<javaModelGenerator targetPackage=\"com.db.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接口生成的位置 -->
		<javaClientGenerator type=\" MAPPER\"
			targetPackage=\"com.db.crud.dao\" targetProject=\".\\src\\main\\java\">
			<property name=\"enableSubPackages\" value=\"true\" />
		</javaClientGenerator>
		<!-- 指定每个表的生成策略 -->
		<table tableName=\"user\" domain Name=\"User\"
			enableCountByExample=\"false\" enableUpdateByExample=\"false\"
			enableDeleteByExample=\"false\" enableSelectByExample=\"false\"
			selectByExampleQueryId=\"false\"></table>
	</context>
</generatorConfiguration>

第二步: 新建application.properties

server.port=8080

#DB Configuration
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

#JPA Configuration
spring.jpa.data =mysql
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
sprign.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.html

第三步:配置mbg.

<?  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\">
		<!-- 配置数据连接信息 -->
		<!-- 	去掉注释suppressDate是去掉生成日期那行注释suppressAllComments是去掉所有的注解 -->
		<commentGenerator>
			<property name=\"suppressDate\" value=\"true\" />
			<property name=\"suppressAllComments\" value=\"true\" />
		</commentGenerator>
		<jdbcConnection driverClass=\"com.mysql.jdbc.Driver\"
			connectionURL=\"jdbc:mysql://localhost:3306/ssm_crud?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8\" userId=\"root\"
			password=\"123456\">
		</jdbcConnection>

		<javaTypeResolver>
			<property name=\"forceBigDecimals\" value=\"false\" />
		</javaTypeResolver>

		<!-- 指定java bean生成的位置 -->
		<javaModelGenerator targetPackage=\"com.db.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接口生成的位置 -->
		<javaClientGenerator type=\" MAPPER\"
			targetPackage=\"com.db.crud.dao\" targetProject=\".\\src\\main\\java\">
			<property name=\"enableSubPackages\" value=\"true\" />
		</javaClientGenerator>
		<!-- 指定每个表的生成策略 -->
		<table tableName=\"user\" domain Name=\"User\"
			enableCountByExample=\"false\" enableUpdateByExample=\"false\"
			enableDeleteByExample=\"false\" enableSelectByExample=\"false\"
			selectByExampleQueryId=\"false\"></table>
	</context>
</generatorConfiguration>

第四步:在测试包下创建mybatis逆向工程测试类 然后运行文件

package com.db.Maven_ssm;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config. .ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MBGTest {
	public static void main(String[] args) throws IOException, Exception {
		 List<String> warnings = new ArrayList<String>();
		   boolean overwrite = true;
		   File configFile = new File(\"mbg. \");
		   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);
	}
}

第五步:F5刷新项目,mybatis逆向工程成功

\"\"

第六步:创建springboot启动类(注意springboot启动类起的包名要高于其他层比如Controller,dao,pojo,service..)

\"\"

package com.db;

import java.util.Properties;

import org.mybatis.spring.annotation.MapperScan;
import org.spring work.boot.SpringApplication;
import org.spring work.boot.autoconfigure.SpringBootApplication;
import org.spring work.context.annotation.Bean;

import com.github.pagehelper.PageHelper;

@SpringBootApplication
//让其扫描dao层接口
@MapperScan(\"com.db.crud.dao\")
public class app {
	//#pagehelper分页配置 
	 @Bean
	    public PageHelper pageHelper() {
	        PageHelper pageHelper = new PageHelper();
	        Properties p = new Properties();
	        p.setProperty(\"offsetAsPageNum\", \"true\");
	        p.setProperty(\"rowBoundsWithCount\", \"true\");
	        p.setProperty(\"reasonable\", \"true\");
	        p.setProperty(\"dialect\", \"ssm_crud\"); // 配置mysql数据库
	        pageHelper.setProperties(p);
	        return pageHelper;
	    }
//启动类
	public static void main(String[] args) {
		// TODO Auto-generated method stub
   SpringApplication.run(app.class, args);
	}

}

 第七步:配置application.yml文件

server:
  port:  8080
spring:
  profiles:
    active: dev
    datasource:
    url: jdbc:mysql://localhost:3306/ssm_crud
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources
mybatis:
  typeAliasesPackage: com.db.crud.bean
  mapperLocations: classpath:mapper/*. 
#pagehelper分页插件
pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql
#注意 “:”后面跟空格,要不会报错

 第八步:service.impl  实现类加入@service注解(dao层 ,service层不需要加注解了)

package com.db.crud.service.impl;

import org.spring work.beans.factory.annotation.Autowired;
import org.spring work.stereotype.Service;

import com.db.crud.bean.User;
import com.db.crud.dao.UserMapper;
import com.db.crud.service.UserService;
@Service(\"userService\")
public class UserServiceImpl implements UserService{
@Autowired //这个注解是属业spring的)或者使用@Resource装配可以减少了与spring的耦合
    UserMapper userMapper;
	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(User record) {
		// TODO Auto-generated method stub
		return userMapper.insert(record);
	}

	@Override
	public int insertSelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.insertSelective(record);
	}

	@Override
	public User selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		System.out.println(\"==============\"+userMapper);
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(User record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(record);
	}

}

第九步:创建Controller类

package com.db.crud.Controller;


import org.spring work.beans.factory.annotation.Autowired;
import org.spring work.stereotype.Controller;
import org.spring work.ui.Model;
import org.spring work.web.bind.annotation.RequestMapping;
import org.spring work.web.bind.annotation.ResponseBody;

import com.db.crud.bean.User;
import com.db.crud.service.UserService;

@Controller
@RequestMapping(\"/user\")
public class userController {

	@Autowired
	UserService userService;
	@RequestMapping(\"/show\")
	@ResponseBody   //返回json数据
	public User login(Model model){
		
		User user=userService.selectByPrimaryKey(1);
		return user;
	}
	
}

最后测试是否成功: 启动spring

\"\"

 

显示数据说明成功了

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

收藏 打印