美文网首页
spring jdbc入门

spring jdbc入门

作者: 搞好关系 | 来源:发表于2019-02-20 17:17 被阅读3次

上一篇我们看来hello入门接着我们添加jdbc链接

配置mysql driver

在applicationContext中配置mysql驱动(可以采用另一种方式将配置单独出来写在文件中)

      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
        <property name="username" value="root"/>
        <property name="password" value="12345678"/>
    </bean>
    <!-- 配置事物管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

Dao层定义操作

@Repository("userDao")
public class UserDao implements IUserDao {
    JdbcTemplate jdbcTemplate;
    @Resource(name = "dataSource")
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public int addUser(User user) {


        int result = this.jdbcTemplate.update("insert into user(name, age) values(?,?)",
                new Object[]{
                        user.getName(),
                        user.getAge()});
        return result;
    }

    @Override
    public List<User> allUser() {
        List<User> allUserResult = this.jdbcTemplate.query("select *from user", new RowMapper<User>() {

            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                String name = resultSet.getString("name");

                int id = resultSet.getInt("id");
                int age = resultSet.getInt("age");
                return new User(name, age, id);
            }
        });
        return allUserResult;
    }
}

依次配置完成Server model,然后我们写出Controller

Action

 @Controller
@Scope("prototype")
@RequestMapping("/u")
public class UserController {
    IUserService userService;
    @Resource(name = "userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/addUser")
    public String user(Model model) {
        User user = new User("jackLee", 30, 1);
        model.addAttribute("user", user);
        int addUserResult = this.userService.addUser(user);
        model.addAttribute("msg", "插入结果:" + addUserResult);
        return "redirect:/u/allUser";
    }
    @RequestMapping("/allUser")
    public String allUser(Model model) {
        List<User> allUserResult = userService.allUser();
        model.addAttribute("allUsers", allUserResult);
        return "allUser";
    }
    @RequestMapping("/json")
    @ResponseBody
    public String json(Model model) {
        List<User> allUserResult = userService.allUser();
        model.addAttribute("allUsers", allUserResult);
        return JSON.toJSONString(model);
    }
}

数据库预览

image.png
user表
image.png
到此我们就可以看出通过浏览器栏查看效果
http://localhost:8080/smvc_war_exploded/u/json
image.png
http://localhost:8080/smvc_war_exploded/u/allUser
image.png

【敲黑板】

1 添加mysql对应jar包

2 检查好连接并确认好是否正确完好添加到对应lib中

相关文章

  • Spring入门(三)之整合jdbc

    三、spring入门之整合jdbc 通过继承org.springframework.jdbc.core.suppo...

  • Spring Boot入门系列(五) 使用Mybatis(Ann

    文章使用版本为 Spring Boot 2.1.x 前言 在 Spring Boot 入门系列(四) 使用Jdbc...

  • spring jdbc入门

    上一篇我们看来hello入门接着我们添加jdbc链接 配置mysql driver 在applicationCon...

  • spring jdbc

    spring jdbc 是什么? spring jdbc 有什么好处? spring jdbc是如何实现的?

  • spring-jdbc

    spring-jdbc快速入门 一、xml配置方式 1、配置xml,用以创建对象 2、编写UserDao

  • spring-jdbc

    spring-jdbc快速入门 一、xml配置方式 1、配置xml,用以创建对象 2.编写UserDao

  • spring学习笔记三-JDBC

    [TOC] spring-jdbc快速入门 一、xml配置方式 1、配置xml,用以创建对象 2、编写UserDao

  • 乐字节-Spring JDBC 和 事务控制

    Spring JDBC 和 事务控制 主要内容 Spring 整合 JDBC 环境 ​ Spring 框架除了提供...

  • Spring框架——Jdbc

    spring-jdbc快速入门 一、xml配置方式 导入包 1.配置xml,用来创建对象 2.编写UserDao

  • spring-jdbc

    spring-jdbc 导包:spring-tx spring-jdbc 一、xml配置方式 1、配置xml,...

网友评论

      本文标题:spring jdbc入门

      本文链接:https://www.haomeiwen.com/subject/utnhyqtx.html