java学习知识积累-初识spring框架的”依赖注入”
控制层 注入式@Autowired
控制层代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.springapp.mvc; import com.springapp.core.FinArticleService; import com.springapp.model.News; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; @Controller @RequestMapping("/") public class HelloController { @Autowired private FinArticleService finArticleService; @RequestMapping(value = "index.html", method = RequestMethod.GET) public String printWelcome(ModelMap model) { List<News> newses = finArticleService.getArticleByPage(); model.addAttribute("newses", newses); return "index/index"; } } |
注入业务service文件 “FinArticleService”,并调用getArticleByPage方法。
业务层代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.springapp.core; import com.springapp.db.NewsMapper; import com.springapp.model.News; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * Created by Administrator on 2015/9/9. */ @Service public class FinArticleService { @Autowired private NewsMapper newsMapper; //前台查询 public List<News> getArticleByPage(){ List<News> lists = newsMapper.selectFinNews(); for(News news : lists){ double length = getLength(news.getTitle()); if(length>16){ news.setTitle(news.getTitle().substring(0,15)+"..."); } } return lists; } //todo 有时间 试下 根据引文判断 public static double getLength(String s) { double valueLength = 0; // 获取字段值的长度,设定两个字节 for (int i = 0; i < s.length(); i++) { // 获取一个字符 String temp = s.substring(i, i + 1); // 判断是否为中文字符 if (temp.getBytes().length == 1) { // 其他字符长度为0.5 valueLength += 0.5; } else { // 中文字符长度为1 valueLength += 1; } } //进位取整 return Math.ceil(valueLength); } } |
业务层又从DAO注入
NewsMapper代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
package com.springapp.db; import com.springapp.model.News; import com.springapp.model.NewsExample; import java.util.List; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface NewsMapper { /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int countByExample(NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int deleteByExample(NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int deleteByPrimaryKey(Integer id); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int insert(News record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int insertSelective(News record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ List<News> selectByExampleWithBLOBs(NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ List<News> selectByExample(NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ News selectByPrimaryKey(Integer id); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByExampleSelective(@Param("record") News record, @Param("example") NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByExampleWithBLOBs(@Param("record") News record, @Param("example") NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByExample(@Param("record") News record, @Param("example") NewsExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByPrimaryKeySelective(News record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByPrimaryKeyWithBLOBs(News record); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table tx_news * * @mbggenerated Wed Sep 09 17:12:11 CST 2015 */ int updateByPrimaryKey(News record); //查询前台文章 List<News> selectFinNews(); //关于我们 List<News> aboutArticle(); } |
该文件由Mybatis自动生成。最后的查询前台文章和关于我们是自己添加。
对应的NewsMapper.xml部分代码:
1 2 3 4 5 6 7 8 9 |
<!--前台文章展示--> <select id="selectFinNews" resultType="com.springapp.model.News" > select a.id as id,a.title as title,a.content as content,a.create_time as createTime,a.user_id as userId, a.category_id as categoryId,a.reference_file as referenceFile,a.general as general,a.keyword as keyword from (select t1.*,(select count(*)+1 from tx_news where category_id=t1.category_id and tx_news.create_time <![CDATA[<]]> t1.create_time ) as group_id from tx_news t1) a where a.group_id <![CDATA[<=]]> 7 ORDER BY a.create_time DESC </select> |
- java学习知识积累-spring框架如何配置进入首页控制器
- java学习知识积累-初练Mybatis