springboot + mybatis + derby 例子

 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot-derby</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
    </parent>



    <dependencies>

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

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

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


        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- alibaba JSON -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.53</version>
        </dependency>
        <!-- spring boot 分页 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.13.1.1</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.31</version>
        </dependency>



    </dependencies>


    <build>

        <!-- 包名 -->
        <finalName>springboot-berby-v1.0</finalName>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <!-- maven 打包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.2.RELEASE</version>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <fork>true</fork><!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
                </configuration>
                <!-- 不加repackage 不能启动 -->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>


        </plugins>
    </build>

</project>

配置文件application.properties

#端口,访问路径
server.port=20208
server.context-path=/

#logging
#日志文件名。例如`myapp.log` 
logging.file.name=./logs/derby.log
logging.level.com.dao=debug
#日志级别严重性映射。例如`logging.level.org.springframework = DEBUG` 
#logging.level=DEBUG,INFO,Console,File


#数据源

spring.datasource.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:derby:./mydb_test;create=true
#spring.datasource.username=admin
#spring.datasource.password=admin



spring.datasource.initialSize=0
#连接池最大使用连接数 
spring.datasource.maxActive=50
#最大
spring.datasource.maxIdle=20
#连接池最小空闲
spring.datasource.minIdle=3
spring.datasource.maxWait=60000



#mapper
mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis.type-aliases-package=com.entity;



# spring mvc
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

# spring mvc 静态资源
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=/WEB-INF/static/

#热部署
spring.thymeleaf.cache=true

Mapper

<?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.dao.UserDao">

	<resultMap type="com.entity.UserInfo" id="resultList">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="passwd" column="passwd" />

	</resultMap>

	<insert id="createSql">
		create schema derbydb
	</insert>
	<insert id="createUserInfoSql">
		create table user_info(
			id int generated always as identity
				constraint user_info_pk
					primary key,
			name varchar(255),
			passwd varchar(255)
		)
	</insert>
	<insert id="insert"  parameterType="com.entity.UserInfo">
		insert into user_info (name,passwd) values (#{name},#{passwd})
	</insert>


	<select id="queryAll" resultMap="resultList">
			select * from user_info
	</select>

</mapper>
Controller 
public class CityController {

    @Resource
    UserDao userDao;

    @RequestMapping("/")
    public String home()   {
        return "spring boot berby ....";
    }


    @RequestMapping("/insert")
    public String insert()   {
        UserInfo userInfo = new UserInfo();
        userInfo.setName("adb");
        userInfo.setPasswd("123123");
        int i = userDao.insert(userInfo);
        return i+"";
    }

    @RequestMapping("/queryAll")
    public String query()   {
        List<UserInfo> userInfos = userDao.queryAll();
        return JSONObject.toJSONString(userInfos);
    }

}

猜你喜欢

转载自blog.csdn.net/jintaocccq/article/details/108572628