spring boot 与 轻量级dao的整合


spring boot


摘要:快速搭建web项目,持久层用注解的方式使用原生sql


一:下载spring boot demo

google 到http://start.spring.io/下载spring boot 的demo 

二:运行调试(返回json)

注意事项:(以idea为开发工具)

1.jdk版本demo里为1.8, 若本地为其他版本,需要改idea的java compiler (setting -- java compiler)

2.pom.xml java version改为所用jdk版本号

3.创建业务类(以User为例)如图所示:

4.代码

@Controller

public class UserController {

        @Autowired

        private UserService userService;

         @RequestMapping("/user")

         @ResponseBody

         public String getUser() {

             return "hello world";

        }

}

注:@Controller与@RestController 的区别

@RestController是在@Controller的基础上在方法return前加上@ResqonseBoby的注解,所以@RestController返回json格式,@Controller返回指定文件

5.启动应用

google --localhost:8080/user

正确输出helloworld

三:返回指定文件(默认配置)


1.指定图片或静态文件

resource文件夹下新建static(存放静态文件)路径为:src/main/resource/static如图:


google -- [localhost:8080/lightDao.png](localhost:8080/lightDao.png)

2.模板引擎  Thymeleaf

Thymeleaf为渲染html,xml的模板引擎,能够分离web前后端,不破坏html的内容,只需要定义标签属性即可,浏览器解析html时没有发现属性自动忽略,所以即可单独访问html文件也可以通过后台返回到模板文件

默认路径 src/main/resource/templates

此路径下新建模板文件index.html

<!DOCTYPE html>

<html>

<head lang="en">

<meta charset="utf-8"/>

<title></title>

</head>

<body>

<h1 th:text="${host}">hello world</h1>

</body>

</html>

pom.xml 引入引擎依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

UserController.java

@Controller

public class UserController {

        @Autowired

         private UserService userService;

        @RequestMapping("/index")

        public String index(ModelMap modelMap) {

                 modelMap.addAttribute("host","www.baidu.com");

                 return "index";

        }

}

重启应用  google --localhost:8080/index

正确输出   www.baidu.com


四.与dao持久层的交互(lightDao)

注:@littlersmall(作者)一个轻量级dao处理框架

不同在于pom依赖包

1.github  down代码引入 如图:


2.pom依赖 :


3.业务类:

User.java

@Data

public class User {

       private int id;

       private String user_name;

}

UserContorller.java

@Controller

public class UserController {

       @Autowired

       private UserService userService;

       @RequestMapping("/user")

       @ResponseBody

       public String getUserById() {

                return  userService.getUserById(1).getUser_name();

      }

}

UserService.java

@Service

public class UserService {

     @Autowired

      private UserDao userDao;

      public User getUserById(int id) {

               return userDao.getUserById(id);

      }

}

UserDao.java

@Dao(dbName = "test")

public interface UserDao {

        @Select("select * from user where id = {id}")

         User getUserById(@SqlParam("id")int id);

}


4.application.properties :(spring boot 根据url可以找到class -name )

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

5.注入DataSource(lightdao约定)

 第一种方式:bean注入

DemoApplication.java(启动应用程序)

@SpringBootApplication

@ComponentScan("com.example")

public class DemoApplication {

       public static void main(String[] args) {

                SpringApplication.run(DemoApplication.class, args);

                System.out.println("--------启动成功---------");

       }

        @Bean(name = "TestDataSource")

        @Primary

        @ConfigurationProperties(prefix = "spring.datasource")

        public javax.sql.DataSource lightDaoDataSource() {

                return DataSourceBuilder.create().build();

        }

}

第二种注入方式:xml

resource文件夹下新建config.xml (请参考spring mvc配置文档)

config.xml

<bean id="TestDataSource" class="org.apache.tomcat.jdbc.pool.DataSource"

destroy-method="close" lazy-init="false">

      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

      <property name="url" value="jdbc:mysql://localhost:3306/test"/>

       <property name="username" value="root"/>

        <property name="password" value="root"/>

</bean>

DemoApplication.java(启动应用程序)只需要加一个注解就ok了

@SpringBootApplication

@ComponentScan("com.example")

@ImportResource("classpath:config.xml")

public class DemoApplication {

        public static void main(String[] args) {

                  SpringApplication.run(DemoApplication.class, args);

                  System.out.println("--------启动成功---------");

       }

}

到此项目完成---w  a   n    完



注意事项  :

1.DemoApplication.java(启动应用程序)  默认package应于user业务类并列,否则扫描不到注解

若自定义package的话  加上注解@ComponentScan("com.example")

2.datasource  bean  name 应为  数据库名称+DataSource 否则找不到数据库 @littlersmall

3.jdbc依赖  

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

spring jdbc  自动获取对tomcat-jdbc的依赖

若引入spring mvc的jdbc依赖   会报异常

4.spring boot 对jsp有限制  参考spring boot jsp支持 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,360评论 6 343
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,295评论 18 399
  • https://www.ibm.com/developerworks/cn/opensource/os-cn-sp...
    大同若鱼阅读 4,991评论 4 18
  • 折腾了一个晚上。早上睁开眼,一缕阳光从窗帘的缝隙里溜进来。啊,我还活着。 都是大姨妈惹的祸。从小大姨妈就特别善待我...
    爱在董监高阅读 273评论 0 0