目录

- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>springdemo2artifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.13.2version>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.3.22version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.3.22version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.2.11version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.10version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>8.0.30version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>2.0.7version>
- dependency>
- dependencies>
-
- <build>
- <pluginManagement>
- <plugins>
-
- <plugin>
- <artifactId>maven-clean-pluginartifactId>
- <version>3.1.0version>
- plugin>
-
- <plugin>
- <artifactId>maven-resources-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.8.0version>
- plugin>
- <plugin>
- <artifactId>maven-surefire-pluginartifactId>
- <version>2.22.1version>
- plugin>
- <plugin>
- <artifactId>maven-jar-pluginartifactId>
- <version>3.0.2version>
- plugin>
- <plugin>
- <artifactId>maven-install-pluginartifactId>
- <version>2.5.2version>
- plugin>
- <plugin>
- <artifactId>maven-deploy-pluginartifactId>
- <version>2.8.2version>
- plugin>
-
- <plugin>
- <artifactId>maven-site-pluginartifactId>
- <version>3.7.1version>
- plugin>
- <plugin>
- <artifactId>maven-project-info-reports-pluginartifactId>
- <version>3.0.0version>
- plugin>
- plugins>
- pluginManagement>
- build>
- project>


- CREATE TABLE account(
- id INT PRIMARY KEY auto_increment,
- name VARCHAR(20),
- money DOUBLE(10,2)
- );
- INSERT INTO account VALUES (NULL,'Mike',888.88),(NULL,'Jock',666.66);

- package com.superdemo.domain;
-
- public class Account {
- private Integer id;
- private String name;
- private Double money;
-
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Double getMoney() {
- return money;
- }
- public void setMoney(Double money) {
- this.money = money;
- }
- @Override
- public String toString() {
- return "Account{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", money=" + money +
- '}';
- }
- }

- package com.superdemo.dao;
-
- import com.superdemo.domain.Account;
-
- import java.util.List;
-
- public interface AccountDao {
-
- void save(Account account);
-
- void delete(Integer id);
-
- void update(Account account);
-
- List
findAll(); -
- Account findByid(Integer id);
- }

- package com.superdemo.service;
-
- import com.superdemo.domain.Account;
-
- import java.util.List;
-
- public interface AccountService {
-
- void save(Account account);
-
- void delete(Integer id);
-
- void update(Account account);
-
- List
findAll(); -
- Account findByid(Integer id);
- }

- package com.superdemo.service.impl;
-
- import com.superdemo.dao.AccountDao;
- import com.superdemo.domain.Account;
- import com.superdemo.service.AccountService;
-
- import java.util.List;
-
- public class AccountServiceImpl implements AccountService {
- private AccountDao accountDao;
-
- public void setAccountDao(AccountDao accountDao) {
- this.accountDao = accountDao;
- }
-
- public void save(Account account){
- accountDao.save(account);
- }
-
- public void delete(Integer id){
- accountDao.delete(id);
- }
-
- public void update(Account account){
- accountDao.update(account);
- }
-
- public List
findAll(){ - return accountDao.findAll();
- }
-
- public Account findByid(Integer id){
- return accountDao.findByid(id);
- }
- }

- jdbc.driver=com.mysql.cj.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/dp1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
- jdbc.username=root
- jdbc.password=123456

- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.superdemo.dao.AccountDao">
-
- <select id="findAll" resultType="account">
- SELECT * FROM account
- select>
- <select id="findByid" resultType="account" parameterType="int">
- SELECT * FROM account WHERE id = #{id}
- select>
- <insert id="save" parameterType="account">
- INSERT INTO account(name, money) VALUES (#{name},#{money})
- insert>
- <update id="update" parameterType="account">
- UPDATE account SET name = #{name},money = #{money} WHERE id = #{id}
- update>
- <delete id="delete" parameterType="int">
- DELETE FROM account WHERE id = ${id}
- delete>
- mapper>

- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd">
-
-
- <context:property-placeholder location="classpath:*.properties"/>
-
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="driverClassName" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- bean>
-
-
- <bean id="accountService" class="com.superdemo.service.impl.AccountServiceImpl">
- <property name="accountDao" ref="accountDao"/>
- bean>
-
-
- <bean class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="typeAliasesPackage" value="com.superdemo.domain"/>
- bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.superdemo.dao"/>
- bean>
- beans>

- import com.superdemo.domain.Account;
- import com.superdemo.service.AccountService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class App {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- AccountService accountService = (AccountService) ctx.getBean("accountService");
- Account ac = accountService.findByid(2);
- System.out.println(ac);
-
- Account account = new Account();
- account.setName("Tom");
- account.setMoney(1314.52);
- accountService.save(account);
-
- ac = accountService.findByid(3);
- System.out.println(ac);
- }
- }