1、创建数据库表结构如下
帐户表
字段名称 |
类型 |
中文 |
长度 |
是否主键 |
是否自增 |
备注 |
id |
Int |
Y |
Y |
|||
account |
Varchar |
帐号 |
255 |
|||
password |
varchar |
密码 |
255 |
|||
coupon |
Int |
游戏点券 |
2、使用spring整合mybatis技术,完成对表中数据的增加和显示
3、使用spring整合mybatis技术,完成用户登录处理
<?xml version="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"> <parent> <artifactId>springFramework</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>day_06_job</artifactId> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring.version>5.3.15</spring.version> </properties> <dependencies> <!-- spring上下文环境 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--druid数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&useSSL=false jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.username=root jdbc.password=zengyu1234
package com.xzit.mapper; import com.xzit.entity.Account; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface AccountMapper { void addAccount(Account account); List<Account> selectAccount(); Account LoginAccount(Account account); }
<?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 namespace="com.xzit.mapper.AccountMapper"> <insert id="addAccount" parameterType="Account"> insert into day_06_account values (null,#{account},#{password},#{coupon}) </insert> <select id="selectAccount" resultType="Account"> select * from day_06_account </select> <select id="LoginAccount" resultType="Account" parameterType="Account"> select * from day_06_account d where d.account = #{account} and d.password = #{password} </select> </mapper>
package com.xzit.service; import com.xzit.entity.Account; import java.util.List; public interface AccountService { void addAccount(Account account); List<Account> selectAccount(); Account LoginAccount(Account account); }
package com.xzit.service; import com.xzit.entity.Account; import com.xzit.mapper.AccountMapper; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class AccountServiceImpl implements AccountService { @Resource private AccountMapper mapper; @Override public void addAccount(Account account) { mapper.addAccount(account); } @Override public List<Account> selectAccount() { return mapper.selectAccount(); } @Override public Account LoginAccount(Account account) { Account account1 = null; try { account1 = mapper.LoginAccount(account); }catch (Exception e) { e.printStackTrace(); }finally { if (account1 == null) System.out.println("登录失败,用户名或密码错误"); } return account1; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--对应db.properties文件--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 自动扫描 --> <context:component-scan base-package="com.xzit"></context:component-scan> <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:SqlMapConfig.xml"></property> </bean> <!-- 对Mapper的扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!-- 配置Mapper文件的基准包 --> <property name="basePackage" value="com.xzit.mapper"></property> </bean> </beans>
<?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> <package name="com.xzit.entity"/> </typeAliases> <mappers> <package name="com.xzit.mapper"/> </mappers> </configuration>
package com.xzit.test; import com.alibaba.druid.sql.ast.statement.SQLPrivilegeItem; import com.xzit.entity.Account; import com.xzit.service.AccountService; import com.xzit.service.AccountServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Account account = new Account(); account.setAccount("曾誉").setPassword("1234").setCoupon(5000); AccountService service = context.getBean(AccountServiceImpl.class); /*service.addAccount(account);*/ /*List<Account> list = service.selectAccount(); list.forEach(System.out::println);*/ Account account1 = service.LoginAccount(account); System.out.println(account1.getAccount()+"登录成功,您的余额为"+account1.getCoupon()); } }
package com.xzit.entity; import lombok.Data; import lombok.experimental.Accessors; @Data @Accessors(chain = true) public class Account { private int id; private String account; private String password; private int coupon; }