4.從零搭建WebApi接口開發(fā)框架-設(shè)計(jì)Dao、Service

這里面采用jdbcTemplate來作為數(shù)據(jù)庫(kù)的訪問功能,dao只簡(jiǎn)單包括用戶的登錄、news的增刪改查。

1、pom依賴

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
    </dependencies>

這里是主要是Springboot的依賴和數(shù)據(jù)庫(kù)的。

1、配置數(shù)據(jù)庫(kù)連接application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot
    username: root
    password: root

2、編寫model

public class User {
    private int id;
    private String username;//
    private String passsword;//
// 忽略set get
}

public class News {
    private int id;//
    private String title;//標(biāo)題
    private String content;//內(nèi)容
// 忽略set get
}

3、編寫dao

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User getByUsername(String username) {
        List<User> list = jdbcTemplate.query("select * from t_user where username = ?", new Object[]{username}, new BeanPropertyRowMapper(User.class));
        return list.size() > 0 ? list.get(0) : null;
    }
}

@Repository
public class NewsDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int add(News news) {
        String sql = "insert into t_news(title,content) values(:title,:content)";
        return new NamedParameterJdbcTemplate(jdbcTemplate).update(sql, new BeanPropertySqlParameterSource(news));
    }

    public int update(News news) {
        String sql = "update  t_news SET title=:title,content=:content   WHERE id=?";
        return new NamedParameterJdbcTemplate(jdbcTemplate).update(sql, new BeanPropertySqlParameterSource(news));
    }

    public int delete(int id) {
        return jdbcTemplate.update("DELETE from t_news where id=?", id);
    }

    public News get(int id) {
        List<News> list = jdbcTemplate.query("select * from t_news where id = ?", new Object[]{id}, new BeanPropertyRowMapper(News.class));
        return list.size() > 0 ? list.get(0) : null;
    }

    public List<News> list() {
        List<News> list = jdbcTemplate.query("select * from t_news", new BeanPropertyRowMapper(News.class));
        return list;
    }
}

4、編寫service

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public User getByUsername(String username) {
        return userDao.getByUsername(username);
    }
}

@Service
public class NewsService {
    @Autowired
    private NewsDao newsDao;

    public int add(News news) {
        return newsDao.add(news);
    }

    public int update(News news) {
        return newsDao.update(news);
    }

    public int delete(int id) {
        return newsDao.delete(id);
    }

    public News get(int id) {
        return newsDao.get(id);
    }

    public List<News> list() {
        return newsDao.list();
    }
}

這一節(jié)比較簡(jiǎn)單,沒有復(fù)雜的內(nèi)容

源碼下載

本例子詳細(xì)源碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容