我这里只做了配置文件版本的,习惯问题
加入mybatis支持
<!-- mybatis支持 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql 支持 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
application.properties文件中增加配置mysql数据源
#配置mysql数据源
spring.datasource.url = jdbc:mysql://localhost:3306/dw_hx?useUnicode=true&characterEncoding=utf8&useSSL=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
创建mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
</typeAliases>
</configuration>
编写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接口实体类 -->
<mapper namespace="com.dw.mapper.UserMapper" >
<!-- 配置返回类型列对应实体类属性映射 -->
<resultMap id="BaseResultMap" type="com.dw.entity.User" >
<id column="ID" property="id" jdbcType="VARCHAR" />
<result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
<result column="USER_PASSWORD" property="userPassWord" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, userName, userPassWord
</sql>
<select id="findByUser" resultMap="BaseResultMap" >
SELECT
*
FROM dw_user
</select>
</mapper>
mapper接口
package com.dw.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.dw.entity.User;
/**
*
* @author DongWei
* 2018年12月4日
*/
@Mapper
public interface UserMapper {
List<User> findByUser();
}
Controller测试
/**
*
* @author DongWei
* 2018年12月4日
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User findByUser(String userName, String password) {
List<User> list = userMapper.findByUser();
System.out.println(list.toString());
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getId());
System.out.println(list.get(i).getUserName());
System.out.println(list.get(i).getUserPassWord());
}
return null;
}
--- controller
@Controller
public class LoginController {
@Autowired
private UserService service;
@RequestMapping("/login")
public String index(){
service.findByUser();
return "login";
}
}