博客管理系统 springboot<2>

个人喜欢写代码之前,新建一个空白页面,一个空白功能。
方便去测试,感觉也不是啥不良习惯,就算是不良习惯又如何。

页面准备用:thymeleaf
说实话,我对jsp还熟悉一点点,对thymeleaf是一点都不懂啊 。但是既然是官方推荐的,好奇来试试。
数据持久化使用Mybatis(不多解释,一个字。。。很好用)。

页面比较好整合直接使用就可以:
在pom.xml中添加依赖


image.png

在resources/templates 新建test.html就可以了

@Controller
@RequestMapping("/user")
public class UserController {
  
  @RequestMapping(value = "/login",method = RequestMethod.GET)
  public String test(Model model){
      model.addAttribute("cc","wwwww");
      return "test";
  }
}

下面我们开始编写service和dao以及mybatis的xml。

public interface UserDao {
    UserBean queryUserByNameAndPwd(@Param("username") String name, @Param("password") String pwd);
}
public interface UserService {

        UserBean queryUserByNameAndPwd(String name, String pwd);
}
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Override
    public UserBean queryUserByNameAndPwd(String name, String pwd) {
        return userDao.queryUserByNameAndPwd(name,pwd);
    }
}

在resources/mapper下建立userMappper.xml




    

    
    
        UPDATE `blob_user`
         
             SET `headimg` =#{headimg}
         
         WHERE `id` =#{id}

    

然后在application中添加MapperScan:

@SpringBootApplication
@MapperScan("com.wz.manage.dao")
public class ManageApplication {

    public static void main(String[] args) {
        SpringApplication.run(ManageApplication.class, args);
    }
}

最后我们在UserController中进行调用

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

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String test(Model model) {
        UserBean userBean= userService.queryUserByNameAndPwd("wz", "wz");
        model.addAttribute("userBean", userBean);
        return "test";
    }
}

现在我们的编码任务已经完成,如果有需要的话可以进行单元测试:


博客管理系统 springboot<2>_第1张图片
image.png

选择Test,新建UserControllerTest;然后我们进行test的编写

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {

    @Autowired
    UserService userService;
    @Test
    public void test1() throws Exception {
       UserBean userBean= userService.queryUserByNameAndPwd("wz","wz");
       System.out.print(userBean.getUsername());
    }
}
image.png

这样表示ok,今天到此为止吧。明天继续,加油。

你可能感兴趣的:(博客管理系统 springboot<2>)