新建maven项目
pom
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shrimpking</groupId>
<artifactId>demo4</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
users
package com.shrimpking.pojo;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/30 21:24
*/
public class Users
{
private Integer id;
private String username;
private String jobs;
private String phone;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getJobs()
{
return jobs;
}
public void setJobs(String jobs)
{
this.jobs = jobs;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone = phone;
}
@Override
public String toString()
{
return "User{" + "id=" + id + ", username='" + username + '\'' + ", jobs='" + jobs + '\'' + ", phone='" + phone + '\'' + '}';
}
public Users()
{
}
public Users(String username, String jobs, String phone)
{
this.username = username;
this.jobs = jobs;
this.phone = phone;
}
}
userMapper
package com.shrimpking.mapper;
import com.shrimpking.pojo.Users;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/31 21:12
*/
public interface UserMapper
{
List<Users> findAll();
}
userMapper.xml
<?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.shrimpking.mapper.UserMapper">
<select id="findAll" resultType="com.shrimpking.pojo.Users">
select * from a_user
</select>
</mapper>
userService
package com.shrimpking.service;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/31 15:03
*/
public interface UserService
{
void show();
void showAll();
}
userServiceImpl
package com.shrimpking.service;
import com.shrimpking.dao.UserDao;
import com.shrimpking.mapper.UserMapper;
import com.shrimpking.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/31 15:04
*/
//@Component
@Service
public class UserServiceImpl implements UserService
{
@Autowired
//@Qualifier("userDao")
//@Resource
//@Resource(name = "userDao")
private UserDao userDao;
//@Autowired
public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
}
@Override
public void show()
{
System.out.println("UserServiceImpl.show:" + userDao);
}
@Override
public void showAll()
{
List<Users> all = this.userMapper.findAll();
for (Users user : all)
{
System.out.println(user);
}
}
@Autowired
public void xxx(UserDao userDao){
//System.out.println(userDao);
}
@Autowired
public void yyy(List<UserDao> userDaoList){
//System.out.println(userDaoList);
}
@Autowired
private UserMapper userMapper;
}
mybatisconfig
package com.shrimpking.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/31 21:04
*/
@Configuration
@PropertySource({"classpath:jdbc.properties"})
@MapperScan({"com.shrimpking.mapper"})
@ComponentScan("com.shrimpking")
public class MybatisConfig
{
@Bean
public DataSource dataSource(
@Value("${jdbc.driver}") String driver,
@Value("${jdbc.url}") String url,
@Value("${jdbc.username}") String username,
@Value("${jdbc.password}") String password
){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean;
}
}
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimeZone=UTC
jdbc.username=root
jdbc.password=mysql123
test
package com.shrimpking.mytest;
import com.shrimpking.config.MybatisConfig;
import com.shrimpking.config.SpringConfig;
import com.shrimpking.service.UserService;
import com.shrimpking.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2024/1/31 18:32
*/
public class AnnoTest
{
public static void main(String[] args)
{
//System.setProperty("spring.profiles.active","test");
// ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
// Object userDao = context.getBean("userDao");
// System.out.println(userDao);
// Object dataSource = context.getBean("dataSource");
// System.out.println(dataSource);
ApplicationContext context = new AnnotationConfigApplicationContext(MybatisConfig.class);
UserService userService = context.getBean(UserService.class);
userService.showAll();
}
}
标签:shrimpking,String,spring,org,Mybaits,import,注解,com,public
From: https://blog.csdn.net/modern358/article/details/135962684