Springboot+Mybatis建立Maven项目

工具:IntelliJ IDEA 2017.2、Maven 3.3.9

一、建立简单项目

1.11.2

 1.31.4

1.5

二、配置pom文件

在此之前要确保Maven设置路径正确

File-Settings-Build-Build Tools-Maven中的Maven home directory设置maven的根目录

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<mybatis.version>3.4.6</mybatis.version>
		<mybatis.spring.version>1.3.2</mybatis.spring.version>
		<pagehelper.version>5.0.4</pagehelper.version>
		<druid.version>1.1.9</druid.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>

		<!--Slf4j注解生成-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.6</version>
				<dependencies>
					<dependency>
						<groupId>tk.mybatis</groupId>
						<artifactId>mapper</artifactId>
						<version>3.3.8</version>
					</dependency>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${mysql.version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

</project>

三、项目结构

generatorConfig.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>
    <!--设置本地目录jar地址 -->
    <classPathEntry location="D:/Maven_Repo/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3" defaultModelType="flat">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/jeffy"
                        userId="root"
                        password="111111">
        </jdbcConnection>

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

        <!-- model 文件生成 -->
        <javaModelGenerator targetPackage="com.example.jeffy.model" targetProject="D:/workspace/jeffy/src/main/java/">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- mapper.xml 文件生成 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="D:/workspace/jeffy/src/main/resources/">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mapper 文件生成-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.jeffy.mapper"  targetProject="D:/workspace/jeffy/src/main/java/">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table schema="joyxuandb" tableName="t_user" domainObjectName="UserInfo"
               enableCountByExample="false"
               enableSelectByExample="false"
               enableDeleteByExample="false"
               enableUpdateByExample="false"
        >
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
            <!--<generatedKey column="id" sqlStatement="select nextVal('EXPENSES_RECORD_SEQ');"/>-->
        </table>

    </context>
</generatorConfiguration>

application.properties配置文件

#配置端口
server.port=8081

#连接数据库四要素
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jeffy
jdbc.username=root
jdbc.password=111111

#mybtais相关
mapper_path=/mapper/*.xml
entity_package=com.example.jeffy.model

 Generator插件生成mapper接口,model对象,mapper.xml

四、Controller、Service

Controller

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public List<UserInfo> selectAll(){
        return userService.selectAll();
    }
}

ServiceImpl(Service接口自行建立)

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> selectAll() {
        return userInfoMapper.selectAll();
    }
}

Mapper(mapper接口自行建立)

<select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_user
  </select>

五、配置类

DataSourceConfig

@Configuration
@MapperScan("com.example.jeffy.mapper")
public class DataSourceConfig {

    /*获取配置数据库四要素*/
    @Value("${jdbc.url}")
    private String jdbcUrl;

    @Value("${jdbc.driver}")
    private String jdbcDriver;

    @Value("${jdbc.username}")
    private String jdbcUser;

    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name="dataSource")
    public DruidDataSource createDataSource() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUsername(jdbcUser);
        dataSource.setPassword(jdbcPassword);
        dataSource.setDefaultAutoCommit(false);
        return dataSource;
    }
}

SqlSessionFactoryConfig

@Configuration
@Slf4j
public class SessionFactoryConfig {

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Value("${mapper_path}")
    private String MAPPERPATH;

    @Value("${entity_package}")
    private String entityPackage;

    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPERPATH);
        sqlSessionFactoryBean.setMapperLocations(resources);
        sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage);
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }
}

六、进行测试

http://localhost:8081/user/list

ending 如有错误,欢迎指出,本人也只是渣渣一枚,就算简单的代码,也要手敲,切勿cv流~ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值